Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to Execute a multiple commands in php using exec() and shell_exec but i am getting a null value back which i shouldn't and nothing is happening (if i copy and paste the strings below in the command line it will work fine and accomplish the job needed) this is the commands i am using:

$command = "cd /../Desktop/FolderName;";
$command .= 'export PATH=$PATH:`pwd`;';
$command .= 'Here i execute a compiler;';

and then i use the escapeshellcmd()

$escaped_command = escapeshellcmd($command);

then

shell_exec($escaped_command);

any ideas what i am doing wrong and i also tried escapeshellarg() instead of escapeshellcmd()?

Solution: the Problem was the permission of the execution compiler for other owners is non and this was the problem. because when you are using exec() function in php the owner of the file will be www-data so you need to give permission for the www-data either from the ACL of ubuntu or whatever linux based operating system(you can know the owner by doing this exec('whoami')), or by the files you need to execute.

share|improve this question
Do you really mean /../Desktop/FolderName? Because that's the same as /Desktop/FolderName, which is probably not what you want. – larsks May 20 '12 at 23:58
it is the same as /Desktop/FolderName and it is what i want but this doesn't effect anything because it is still changing the directory to the folder i need – dori naji May 21 '12 at 0:01
i would always use the full, not relative, path for such things. – Dagon May 21 '12 at 0:01
why would you escape the command? isnt the entire point to execute the command? escaping prevents execution. – chris May 21 '12 at 0:03
i have tried without escaping but still the same problem is occurring – dori naji May 21 '12 at 0:04

1 Answer

up vote 0 down vote accepted

(Sorry my bad English) On Linux you can add your Commands in a Shell Script.

You can put this in any file:

#!/bin/bash
cd /../Desktop/FolderName
export PATH=$PATH:`pwd`
EXECUTE COMPILER

And save this as fille.sh

Then, add execution permissions: chmod +x path/to/file.sh

From PHP, you can call this Script executing: shell_exec('sh path/to/file.sh');

Hope this helps!

share|improve this answer
it is still not working, should i enable shell_exec() function from somewhere ?? – dori naji May 21 '12 at 0:19
Have you tried executing the Shell Script rfom outside the PHP Script? – Yefb May 21 '12 at 7:43
yes and it works fine. i am researching and i found some people talking about enabling and disabling the exec() function. if the safe mode is on but the safe mode is off in my php.ini. and it is still not working – dori naji May 21 '12 at 9:22
And still not working using using system()? (It's strange) – Yefb May 21 '12 at 17:22
Tell me if PHP is showing warnings or errors. If not, look at PHP Error Logs. – Yefb May 21 '12 at 17:33
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.