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.
Related
I have a Python script which is encoding a video and then calling a shell script which uploads the new video to dropbox. It works fine from the command line but I needed to make it so others could execute it so I have a PHP script calling the python script.
I don't want the PHP script to run forever (it takes 15-30 mins for it to complete), I just want it to kick off the python script and be done. I figured out what I need to make that happen and like I said it works on the command line. But when it is called via PHP, the video encodes but the file never uploads. I can see the dropbox script was kicked off and is listed as a process using some percent of CPU, that percent never changes, it seems stuck/dead.
the command looks like this, being run using cmd()
script.py -options &>/logs/phptopython.log &
The shell script is kicked off using Popen
Any suggestions?
thanks
It sounds like this could be a permissions issue. Double check the permissions on the directory to which you are trying to upload the video. If you are on Linux you can modify the permissions on that directory like this:
chmod 755 /path/to/dir
This gives the file owner read, write and execute permissions (7). The group and other users get read and execute permissions (5).
Apache is likely running as a different user than when you run the command yourself in bash. A quick test to see if it's a permission issue would be to grant 777 on that directory. I wouldn't leave it that way though – it'd just be a way to quickly identify if permissions are the issue.
If the script works with 777 permissions, you could either change the owner of the directory to the user Apache runs as or add the Apache user to the directory's group and grant the group write permisssions.
Edit:
I just noticed you said you use cmd(), so I'm guessing you are on Windows. My comments might still be relevant but the chmod command won't work on Windows.
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.
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'm trying to configure my local development environment to read .less files so that I can edit .less files during development and only convert to .css when it's time to go live. Make sense?
I'm running MAMP as my local testing server. I'm following the instructions I found here:
http://programming-perils.com/155/parse-less-files-on-the-fly-and-serve-them-as-css/#comment-920
In short, the plan is to use an htaccess file catch requests to .css files and direct them to a PHP script which compiles the .less file of same name and returns the css code.
Everything seems to be working from the command line. I can compile a .less file from the command line and it spits out the css. I know my rewrite rule is working because I can type the url into a browser and see the output of my php script. For example, if my PHP script calls echo shell_exec('pwd'); I will see a path printed in the browser.
THE PROBLEM is that I can't get the less script to run unless I SSH to the localhost as root. When I exit SSH and run the command I get "Permission denied". I suspect this is what happens when my PHP script tries to call this... so it's returning nothing.
I guess the question boils down to how can I get my PHP script to run the less compiler?
UPDATE! I solved the problem...
It turns out that the less command (path/path/lessc) needed to be sudo'ed. PHP wasn't doing this, so the shell_exec() command wasn't returning anything. That's why my echo statements DID work.
There are a lot of ways to sidestep this, but I determined that editing the list of sudoers with sudo visudo was the best for my purposes. There was a lot of helpful tips on this post. Through trial and error, I figured out that PHP uses the www-data account. Adding this line fixed my problem:
www-data ALL=(ALL) NOPASSWD: /var/root/node/npm/node_modules/less/bin/lessc
Something to remember is that you STILL have to add sudo to the command that gets fed to shell_exec(). Hope this is helpful to someone else.
Maybe it would be easier if you'd use the PHP implementation of lesscss: lessphp
It turns out that the less command (path/path/lessc) needed to be sudo'ed. PHP wasn't doing this, so the shell_exec() command wasn't returning anything. That's why my echo statements DID work...
See my edits to the question above.
Scenario:
I have a php page in which I call a python script.
Python script when run on the command line (Linux) shows output on the command line, as well as writes the output to a file.
Python script when run through php, doesn't do either.
Elaboration:
I use a simple system command in PHP to run the python script as:
/var/www/html/1.php:
system('/usr/python/bin/python3 ../cgi-bin/tabular.py 1');
/var/www/cgi-bin/tabular.py
--This python file basically parses a data file, uses python's regular expression to search for specific headings and outputs the headings to the stdout, as well as write it to a file.
This python script has a few routines in it which get executed, so I put print statements to debug. I noticed only a few initial print statements' output in the PHP page, all the ones from the function that actually does something are not seen.
Also, as part of my test, I thought well the py script is in a different folder so let me change it to the /var/www/html folder, no go.
I hope I captured the problem statement with sufficient detail and someone is able to reproduce this issue at their end. If I make any progress on this one myself, I'll annotate this question. Thanks everyone.
Gaurav
I bet your py script has some bug which couses it to break when called from inside PHP.
Try
passthru('/usr/python/bin/python3 ../cgi-bin/tabular.py 1 2>&1');
to investigate (notice 2>&1 which causess stderr to be written to stdout).
A permission problem is most likely the case.
If apache is running as apache, then it will not have access to write to a file unless
The file is owned by apache
The file is in the group apache and group writable
The file is world writable
This is a "sticky" problem on a multi-user machine, as different people have access to Apache.
Try chmod 666 output.txt on the file and then re-run your test.
Considerations:
Have the python script write the output to a database
Use PHP's popen functionality to open the process and communicate over pipes
Re-write using PHP's regular expressions
Write the output file to /tmp and then read the results using PHP as soon as the python script is done.
etc...
Check that the user the python script is running is has write permissions in CWD. Also, try shell_exec() or passthru() to call the script, rather than system().