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.
Related
I am trying to run a .sh file from php.
I tried doing it with shell_exec(). but its not working
I refered many questions related to this in stack overflow but could not solve
my php code is(web.php)
<?php
echo shell_exec('/var/www/project/xxe.sh');
echo "done";
?>
only done is printed. but it is working from terminal(php /var/www/project/web.php)
In xxe.sh I am calling a python file
python vin.py
I have also changed the file permission to 777 for both .sh n .py files
please help
If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().
See http://www.php.net/manual/en/ini.core.php#ini.disable-functions
Your apache's php.ini file may look something like
disable_functions=exec,passthru,shell_exec,system,proc_open,popen
Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.
shell_exec might not know what directory to look in for your executable's location directory. What solved it for me was this before the shell_exec:
putenv('PATH=/usr/local/bin');
Then the terminal can find the executable. Also check permissions on every part of the command to make sure apache user has read and execute permissions.
If it works well in shell, I think apache is chrooted. So php can't find /var/...
Or user of httpd user does not have permission to enter /var/...
If you are good at PHP. Open dir /var/... And readdir() and check dir exists and check file exists.
This question might help you. scanning /home/ with opendir()
The problem is usually that when you exec code from within php it is run as the webservers user www-data in alot of linux distros. Normaly this user does not have an enviroment set up, and because of that no PATH. By using full paths in your files you can usually overcome this.
xxe.sh
/usr/bin/python /path/to/script/vin.py
While trying to run a script triggered by github post-receive webhook.
Here is where my project directory is located(cloned git repo):
/var/www/html/my-repo
I create a script inside the above directory called webhook.php:
<?php
#webhook.php
$cmd = shell_exec("git pull 2>&1");
#for debugging
echo $cmd;
?>
Execute the following command inside /var/www/html
sudo chown www-data:www-data -R my-repo/
Test it by going to http://www.myserver.com/my-repo/webhook.php
Add the path to your script to github webhooks.
I have been stuck in this problem for several hours.
I have thought about a solution.
1. move your script to a python file "script.py" and place this file to your server root.
2. shell_exec("python script.py");
Any way, it works for me.
On my host I had to give a different path for my php file to be executed from shell_exec().
This didn't work shell_exec('/usr/bin/php backgroundtask.php');.
While this did shell_exec('/opt/php/php-5.5.0/bin/php backgroundtask.php');.
You can visit this Reference.
I had the same issue because PHP backslashes.
PHP escapes the backslashes, so the command that reaches the shell
'COPY E:path1\path2\file.prn /B \127.0.0.1\"PRINTER NAME"'
so I gave command like this
'COPY E:\\path1\\path2\\file.prn /B \\\\127.0.0.1\"PRINTER NAME"'.
You have to double-escape the backslashes: once for PHP and once for the shell.
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 wrote a PHP script, which is creating some files.
After it finished i want it to open a folder, where the created files stored.
exec("explorer C:\\test");
Unfortunately it isn't open the folder at all.
I check it in cmd as well where it is working.
I also tried this:
shell_exec("explorer C:\\test");
Any hint or advise is greatly appriciated.
This can be done if your localhost is on your computer with windows operating system (I use xampp). Any cmd command will work with exec();
So for your question:
$path = "C:\your path";
exec('start "" "'.$path.'"');
Will open your folder in windows.
Why not use PHP's directory functions?
http://php.net/manual/en/function.dir.php
http://php.net/manual/en/function.readdir.php
I'm guessing that PHP can't open a GUI program, such as explorer, as it may not be able to access the user's display. Just my theory anyway. You could build a folder GUI using PHP and the aforementioned functions.
I'm using flare to decompile a flash movie. I need to do it via PHP. User visits PHP script, it decompiles the flash movie.
Here is how the files are set up on my web host:
index.php:
<?php
error_reporting(-1);
echo shell_exec('./flare movie.swf');
?>
Flare is supposed to create a file named movie.flr once the script runs, but there is no such file. I am thinking my shell_exec is wrong.
phpinfo() - http://zachafer.com/phpinfo.php
You're trying to execute the wrong file.
flare.tgz is an archive (not executable) containing the flare binary (executable).
Unpack it with an archiver tool like winrar on windows or tar on linux (tar xvf flare.tgz).
Inside the archive, you will find a file named flare. Try with this one and it might work if your host provider allow the shell_exec() function calls.
You can't execute a .tgz file directly! You need to unpack the executable before you can run it.
This function is disabled when PHP is running in safe mode... check it or ask your host provider about it
I think flare.tgz is not an executable but a .tar.gz file (it's like a zip file, but it is another format).
You have to unpack it first !