I've made an extension for my web application in php. I want the php script to remake the extension, so I'm trying to send a command to the terminal. It looks like this:
shell_exec("make -C /folder1/folder2");
And it won't make the .so file no matter what. I've used chmod 777 on that folder to make it editable but it didn't help. When I'm directly using the command in the terminal it works just fine:
make -C /folder1/folder2
The .so file gets created without a problem, functions from my extension work like they should. Does anyone have a solution for this?
Make sure that PHP is not running in safe mode and that the PHP user has read/write access to the files and folders. Read the comments at shell-exec for more information.
Related
I can't get exec() to read or write how I need it to:
exec("php -f /root/script/screenshot.php")
Fails with Could not open input file. Must be a permissions problem. Changing the file owner to the php user didn't fix it though.
Can't get this to work either:
exec("xvfb-run -a cutycapt --min-width=1920 --min-height=1920 --url='{$url}' --out='{$path}'");
Fails but doesn't give any error message. cutycapt is a screenshot app to take a screenshot of a website.
Both commands work perfectly from command-line. But it seems that php exec() will neither read nor write files...
watch if your folder path is in the sudoers file
Figured out that it was indeed a permissions error. The second command wasn't working because the folder didn't have write permissions to the apache user.
exec("xvfb-run -a cutycapt --min-width=1920 --min-height=1920 --url='{$url}' --out='{$path}'");
Changed the permissions, and now the above command is saving screenshots no problem at all.
My setup is as follows: Windows 7, XAMPP with Apache and PHP enabled I have a PHP script in which I call an external program to do run a conversion. This external program is an EXE file, which requires 3 attributes:
The source file
The destination file
Additional flags (conversion type etc)
When I use the command line tool built into XAMPP to execute my script, everything works fine. But when I use the exec() function in my PHP script, no output file is created. I'm pretty sure the conversion is actually happening (it takes about 5 seconds, about the same time it takes to run the PHP script).
I think it's a permissions thing, so I already moved the EXE file to the same folder as my PHP file and adjusted the permissions of the entire folder (I granted all permissions to all users). I also disabled the Windows UAC and tried to put the command in a BAT file. The file just is not created.
Any help or tips would be greatly appreciated!
My PHP code is as follows:
exec('c:\converter.exe c:\src.txt c:\dst.txt -f', $output);
print_r($output);
When I print out $output, the array turns out to be empty. When I put the exact same command in Command Prompt, the code works like a charm (no syntax errors). I use absolute paths as well.
Try to copy your executable file in same folder as your application.
try
exec("script.exe src.txt dst.txt", &$output);
echo $output;
also, do not forget to use escapeshellcmd() to add some security to your application.
Thank you very much for your input! As it turns out, it was Windows issue caused by the 'Interactive Services Detection' feature. Apache was running as a system service, which prevented calls to external programs (with a GUI). I disabled the run-as-service feature in XAMPP, which solved the problem. A more thorough explanation can be found here: http://php.net/manual/en/book.exec.php
I've searched for help and tried everything on this thread, but still can't make this work. I'm trying to run the sox (Sound eXchange) command line utility from my PHP script using shell_exec(). I need to concatenate two audio files, both of which are in the same directory as the PHP script (i.e. accessible to apache).
Here's the confusion:
Some sox commands work fine using shell_exec(). For example I can play an audio file or retrieve information about it.
If I echo out the concatenation command (so I know exactly what PHP is sending to the shell) and then copy and paste it into a shell window, it runs perfectly.
My apache user (_www) has full rights to the directory where the PHP script is, as well as the sox directory. I ran "chown -R _www:_www" and "chmod -R 777" on both directories.
I'm using the full path to the sox executable and the audio files.
So this works in the shell:
"/soxpath/sox /filepath/file1.wav /filepath/file2.wav /filepath/combined.wav"
But this doesn't work from PHP:
shell_exec('/soxpath/sox /filepath/file1.wav /filepath/file2.wav /filepath/combined.wav');
Can anyone shed some light on this? What am I missing? Thanks.
Okay, I got it working finally so I thought I should clear up any confusion I created. The problem was something unrelated. So, yes, you can run sox commands from PHP using shell_exec().
I was simply running into a timing issue with javascript. I was using wami recorder to capture audio on the client side and then save the audio file on the server. So my PHP script was in the context of an ajax call handler, which by definition is asynchronous. Should have realized that earlier.
The issue was that the file was not done saving when the sox command to concatenate was run, so naturally it failed because the file didn't exist yet. When I made the call synchronous it worked.
i am trying to run this piece of php code on my server:
<?php
$cmd = 'echo "this is a test" > /home/ubuntu/scripts/test_file';
echo exec($cmd);
?>
From my understanding it should add the piece of text to the file test_file . The file exists in the appropriate location and i have tried chmod 755 and chmod 777 on the php file. But i dont see the text being added to the text_file . I tried running the linux command directly on the server and it works. Could some one tell me what i am doing wrong?
Also, i am trying to create a virtual host file on the server through a php script. Rather than running the commands through php exec() , i thought it would be better to run a shell script, with the shell script reading the required parameters from a text file and setting the directory path in the virtual host file. I am new to linux, is this a good approach or is there a better way in going about this? All this is being done to setup a magento based site programatically. Thanks.
Your code is OK. The problem probably either lies with your php being in safe mode (though it's deprecated, see link) or with file/directory permissions.
No need to give the file permissions 0777 since that makes the file executable, 0666 should suffice. It is not enough however for the file to have the right permissions, each directory on the path must be traversable. Try a different directory to which the user with whose privileges the php code runs has access, /tmp is a good start.
General way to debug problems like this is to execute a different command which gives you extra information about the context in which echo is executed, e.g.
<?php
echo exec("id");
echo "<br/>";
echo exec("ls -l /home/ubuntu/scripts/test_file");
?>
(remember exec() only returns the last line of command's output, these display just one line though). These commands will tell you the user which runs the code and whether they can see the file at all.
As the comment already said: this is actually bad way to accomplish what you're trying to do, as writing Apache configuration based on user input through web could open you up to multiple issues.
What you might consider, is to have the PHP side write the required information to a file, or a database, which is then polled every now and then via a cron script or similar by a different process that does the actual configuration changes. This eliminates the need to exec() from PHP (which is always bad). With this, your process that runs PHP wouldn't need to have write permissions to important system files.
I simply want to 'rar' a folder with the help of PHP. There are 2 ways to do this. One is via shell_exec or exec, which isn't working for me, although shell_exec and exec are enabled on the server and working for other commands.
The other method is via .sh file, but I don't know how to use it properly :(
I need some code which works properly for this.
I'm trying to use this command:
rar a -v100m -m0 /home/admin/somefolder.rar somefolder-to-rar
It's Ubuntu 9.10
if other shell commands work then rar should.
is rar.exe on the path on the machine? or are you specifying the full path to rar.exe in your command?
Check the working directory, and try using passthru to display any error from the output
With php you can use backticks (``) to execute a command (from php.net)
I'm not sure what it would be to rar, but zip would be:
<?php
`cd $dirToZip; zip -pr $nameOfZipFile *`
?>
Assuming your command is correct and rar is accessible from cli the backticks should work.
You likely have a permissions problem. Check to make sure that whatever user PHP is running as has access to execute RAR.
Also, follow Sam's suggestion of using the full path. Your standard path may be specific to your user account, which may be different for the user PHP uses.