Solved

Running linux shell commands from PHP - How to use escaped parenthesis in exec() ?

Posted on 2006-11-14
20
675 Views
Last Modified: 2013-11-18
I am trying to execute this command via PHP exec() function:
convert rose: \( +clone  -background navy  -shadow 80x3+5+5 \) +swap  -background none   -mosaic   shadow.png

The command is working well when I execute it in shell (Linux). However, I cannot get it working in any of exec(), shell_exec() or system()...
I have tried the following, without any success:

test-1:
$command = "convert rose: \\( +clone  -background navy  -shadow 80x3+5+5 \\) +swap  -background none   -mosaic   shadow.png";
exec($command);

test-2:
$command = "convert rose: ( +clone  -background navy  -shadow 80x3+5+5 ) +swap  -background none   -mosaic   shadow.png";
exec(escapeshellcmd($command));

These are the shell command line functions for ImageMagick. I can execute other complicated commands from PHP exec() *IF* they don't have any parenthesis.
If I don't escape parenthesis, linux throws a syntax error so I have to put backslashes before parenthesis, from PHP, but how ?
0
Comment
Question by:Buraque
  • 8
  • 6
  • 4
  • +1
20 Comments
 
LVL 9

Accepted Solution

by:
blue_hunter earned 500 total points
ID: 17944580
if you trying to execute a linux command via PHP.

lets say, you using "convert"

you would need to know the full path/ location where the "convert" is. (normally, it would be in /usr/bin/)
 
the command would be like
'/usr/bin/convert rose: ( +clone  -background navy  -shadow 80x3+5+5 ) +swap  -background none   -mosaic   shadow.png'



for your parenthesis problem,
i would suggest you to include the command with single quote instead of double quote
example:

'/usr/bin/convert rose: ( +clone  -background navy  -shadow 80x3+5+5 ) +swap  -background none   -mosaic   shadow.png'


0
 
LVL 1

Author Comment

by:Buraque
ID: 17944934
Thanks for the comments blue_hunter...
As I mentioned, I can use convert command if the command line have no parenthesis.
Convert and other ImageMagick commands are accessible from anywhere in entire server.

And single quotes are also not working.
0
 
LVL 3

Expert Comment

by:zanus123
ID: 17945028
have you tried using shell_exec()
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Buraque
ID: 17945068
Yes zanus,
As I mentioned in my question, I have tried all exec(), shell_exec() and system() functions...
Thank you.
0
 
LVL 3

Expert Comment

by:zanus123
ID: 17945119
oh...didn't see that

does the exec command just return false....any errors.....any output whatsoever

try this and see what it gives you

$command = "convert rose: \\( +clone  -background navy  -shadow 80x3+5+5 \\) +swap  -background none   -mosaic   shadow.png";
exec($command, $output);
print_r($output);
0
 
LVL 3

Expert Comment

by:zanus123
ID: 17945159
or you could also try using echo with backticks

$command = `convert rose: ( +clone  -background navy  -shadow 80x3+5+5 ) +swap  -background none   -mosaic   shadow.png`;
echo $command;
0
 
LVL 1

Author Comment

by:Buraque
ID: 17945173
Good idea!

tried it, and it returned an empty array.
Which most probably means convert command is working but cannot recognize the parenthesis. So cannot perform the task correctly and returns an empty value ??.
0
 
LVL 1

Author Comment

by:Buraque
ID: 17945180
This web site must have "Edit Post" button grrr...

I wasn't thinking... convert command actually doesn't return anything... So empty array is pretty expected value.
0
 
LVL 3

Expert Comment

by:zanus123
ID: 17945209
I haven't really used linux in a while but does the stderr automatically get sent to stdout

If not you could try redirecting it and run the same $output array method

$command = "convert rose: \( +clone  -background navy  -shadow 80x3+5+5 \) +swap  -background none   -mosaic   shadow.png 2>&1";
exec($command, $output);
print_r($output);
0
 
LVL 9

Expert Comment

by:blue_hunter
ID: 17945265
had you tried execute the command with system()?


or modified version

$command = "convert rose: \\\( +clone  -background navy  -shadow 80x3+5+5 \\\) +swap  -background none   -mosaic   shadow.png 2>&1";
exec($command, $output);
print_r($output);


0
 
LVL 1

Author Comment

by:Buraque
ID: 17945302
Sorry not working...

I just cannot get it...
I started to suspect PHP. Is this a bug or something?
PHP cannot sent a proper backslash character to the server?
0
 
LVL 6

Expert Comment

by:aescnt
ID: 17945332
()'s dont need to be escaped in PHP, system("echo testing (parentheses)"); shows the right result for me.
Backslashes do need to be escaped in PHP strings though: system("echo testing \\backslash\\"); shows "testing \backslash\" for me.

So why is it "not working" -- what type of error did you get?
0
 
LVL 3

Expert Comment

by:zanus123
ID: 17945360
just wondering, but what happens when you use 3 backslashes......
0
 
LVL 9

Expert Comment

by:blue_hunter
ID: 17945370
(before)      ----->      \\\(    
(after)        ------>      \(

0
 
LVL 6

Expert Comment

by:aescnt
ID: 17945383
To answer zanus:

simply 3 backslashes (like "\\\") will produce a parse error.
However, if you're using 3 backslashes and a character after it "\\\(", it's like combining "\\" and "\(". the first results in a backslash, the 2nd results in (. So:
echo "\\\("; // result: \(

Also if you use something like "\\\n" that will be the same as "\\" and "\n", of course \n being the newline meta character:
echo "\\\n"; // result: a backslash followed by a new line
0
 
LVL 3

Expert Comment

by:zanus123
ID: 17945414
hehe....I knew what it did already..
I was actually asking Buraque how it affected his query..

but thanx for enlighting me though.
0
 
LVL 1

Author Comment

by:Buraque
ID: 17945448
New info:

It may not be an issue with the parenthesis at all...

I tried this simple command:
exec("convert /home/stcom/public_html/test/shadow.png  /home/stcom/public_html/test/shadow.jpg");
It is working both from linux shell and from PHP...

But this command:
exec("convert /home/stcom/public_html/test/sample1b.png +clone -background green -shadow 80x3+5+5 +swap -background none -mosaic /home/stcom/public_html/test/shadow.png");
Working from linux shell, but not produce any results from PHP...
0
 
LVL 1

Author Comment

by:Buraque
ID: 17945591
Veeerrry confusing...
this complex command also working in both PHP and Shell

exec("convert -size 320x85 xc:transparent -font /home/stcom/fonts/ariblk.ttf -pointsize 72 -draw \"text 25,60 'MiMi'\" -channel RGBA -gaussian 0x6 -fill darkred -stroke magenta -draw \"text 20,55 'MiMi'\" /home/stcom/public_html/test/fontfuzzy112.png");
0
 
LVL 1

Author Comment

by:Buraque
ID: 17948776
Finally!!

I found that I have 2 convert commands in my server. First one is in /usr/bin version 6.0, second one is in /usr/local/bin version 6.3
PHP sees the first, bugged  one, in the path, linux sees the second one...

When I entered the path to the exec command, problem is solved
exec("/usr/local/bin/convert -size 320x85 xc:transparent.........");

Blue_hunter mentioned about it first, so points goes to him/her :)
0
 
LVL 9

Expert Comment

by:blue_hunter
ID: 17952715
cool! glad to hear that you solved your problem.

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question