Trouble running a script under PHP, permissions

A plethora of command-line scripts that perform geometric transforms, blurs, sharpens, edging, noise removal, and color manipulations.
Post Reply
bHX3IA1COI4bHcK
Posts: 6
Joined: 2014-02-21T12:04:56-07:00
Authentication code: 6789

Trouble running a script under PHP, permissions

Post by bHX3IA1COI4bHcK » 2014-03-14T15:01:47-07:00

Hello, all,

Basic problem: I can run the Similar script just fine from the terminal as me or root, but local PHP scripts can't (running as user www). How can I enable the user www to run Similar and ImageMagick CLI commands using PHP scripts like:

Code: Select all

$string_to_exec = "/usr/local/bin/similar " . $image_1_path . " " . $image_2_path . " 2>&1";
$output = shell_exec($string_to_exec);
such that normal user (me) doesn't lose access to Similar if I chown it to user www?

hopefully helpful:
OSX 10.9.2
Apache2
PHP 5.4.24
ImageMagick 6.8.7.7
Imagick PHP module 3.1.0RC2

I'm looking forward to using phash, but my version of ImageMagick came by way of Homebrew, so it isn't the latest version.
Thank you!

User avatar
fmw42
Posts: 25046
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble running a script under PHP, permissions

Post by fmw42 » 2014-03-14T16:11:25-07:00

what do you get for messages if you run it as (exec with $out to get messages rather than shell_exec)

<?php
exec("/usr/local/bin/similar image1 image2 2>&1",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>


Have you implemented my Pointers on my home page for use with PHP?

bHX3IA1COI4bHcK
Posts: 6
Joined: 2014-02-21T12:04:56-07:00
Authentication code: 6789

Re: Trouble running a script under PHP, permissions

Post by bHX3IA1COI4bHcK » 2014-03-14T17:28:37-07:00

The output is as follows:
sh: /usr/local/bin/similar: Permission denied
when I, not www, chown the Similar script

Is this something doable with user groups? Can I make a group with {username} and www own the script somehow?

Also, similar is added to my path correctly. I did go back, based on your notes, and changed the working directory from "." to "/tmp" for grins, but got the same permission denied message.

User avatar
fmw42
Posts: 25046
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble running a script under PHP, permissions

Post by fmw42 » 2014-03-14T17:55:59-07:00

Either your directory that holds the script is not allowing access to run the script or the script needs executable permissions. The latter is the most likely situation.

chmod u+x similar

I would not try to run the script from /usr/local/bin. Put the script into your working directory.

/usr/local/bin is the directory holding IM convert. Be sure that is in your PATH. Or modify the script as per the instructions on my home page.

bHX3IA1COI4bHcK
Posts: 6
Joined: 2014-02-21T12:04:56-07:00
Authentication code: 6789

Re: Trouble running a script under PHP, permissions

Post by bHX3IA1COI4bHcK » 2014-03-14T18:08:26-07:00

Hmm.

Code: Select all

sudo chmod u+x
did not by itself change anything. I'll put a copy of the script into the working directory and see if that helps. I appreciate your guidance, Fred.

EDIT: I mv'ed similar to the working directory, now I get

Code: Select all

/Users/MyUserName/Sites/similar: line 153: convert: command not found
/Users/MyUserName/Sites/similar: line 154: convert: command not found
/Users/MyUserName/Sites/similar: line 161: charArr1: bad array subscript
/Users/MyUserName/Sites/similar: line 162: charArr2: bad array subscript
/Users/MyUserName/Sites/similar: line 186: convert: command not found
/Users/MyUserName/Sites/similar: line 199: [: : integer expression expected
/Users/MyUserName/Sites/similar: line 204: [: : integer expression expected
/Users/MyUserName/Sites/similar: line 210: [: : integer expression expected
/Users/MyUserName/Sites/similar: line 224: convert: command not found
, which all looks like a bad link to ImageMagick's convert binary.

Tried various combinations of moving and symlinking convert between the working directory and /usr/local/bin , but no joy yet.

Oh, dang it, convert wasn't where I thought it was. /opt/ImageMagick/bin/convert

EDIT2: Ok, rolled through the similar script and added /opt/ImageMagick/bin/ everywhere I found convert. Now the script is at least finding the binary OK. It is also thinking it is working on images from /Library/Webserver/Sites/ instead of ~/Sites/

This UNIX stuff squeezes my brain.

EDIT3: Okie doke, that did it. Had to go through and add the full path to convert all throughout the similar script, but it is working like a charm now via PHP.

Thanks again for a great script, Fred!

User avatar
fmw42
Posts: 25046
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trouble running a script under PHP, permissions

Post by fmw42 » 2014-03-14T18:59:13-07:00

EDIT3: Okie doke, that did it. Had to go through and add the full path to convert all throughout the similar script, but it is working like a charm now via PHP.
You did not have to do that. You just needed to follow the instructions on my home page:

Code: Select all

edit the script somewhere between the comments and the first use of any IM command, such as just below the defaults section to add the following two lines:
imdir="path2"	#(such as imdir="/usr/local/bin" or imdir="/usr/bin")
PATH="${imdir}:${PATH}"

Post Reply