tar utility, PHP and exec - php

Consider the following code
echo "tar -cvf $outpath -C $path $file<br>";
$arr = array();
$val = exec("tar -cvf $outpath -C $path $file", $arr);
var_dump($arr);
echo outputs tar -cvf ../folder/subfolder1/1234.tar -C ../folder/ subfolder2 and this command really create archive if I call it from command line from htdocs folder. However exec do nothing. var_dump($arr) prints empty array. So here are the questions
How can I make exec create archive?
Why empty array is outputed?
UPD. please note, that I'm running windows.

For those who'll read this question: adding 2>&1 to command helped. And I used shell_exec instead of sheel
$val = shell_exec("tar -cvf $outpath -C $path $file 2>&1");
echo "$val"; // prints output from tar

Related

How to run for loop that is inside shell script file from PHP file?

I have a a shell script (myscript.sh), which contains many bash commands. I want to run these command from a php file (index.php).
I used exec to execute the commands as following:
$contents = file_get_contents('../folder/myscript.sh');
$output = null;
$return_var = null;
exec($contents, $output, $return_var);
print_r($contents);
print_r($return_var);
print_r($output);
Here is the content of myscript.sh
VAR1=("somePath/allDirs")
ls
cd ..
ls
for DIR in "${VAR1[#]}"; do
echo "Name is $DIR"
done
pwd
....... other 5 lines of commands
All commands before the loop is working i.e: ls and cd .. and I got the output. However, the for loop is not working and it stop executing the rest of the commands after it.
My question is: how to execute this for loop that is inside shell script file from php file?
note: the for loop contains more command here I just put the echo to make the code easy to read and clear.
Are you mutating the contents of the script at all? Why not just execute the script file rather than passing in the contents of the script? Regardless, if you must pass the contents directly to exec, pass it to bash -c and escape the command contents:
$contents = file_get_contents('../folder/myscript.sh');
$output = null;
$return_var = null;
$contents = escapeshellarg($contents);
exec("bash -c $contents", $output, $return_var);

Mount remote file system using sshfs with a PHP script

I want my PHP script can mont a remote system with sshfs command.
But it doesn't seem to work, the folder has been created but the folder still empty after execution. I also tried with a user without SU and it was working fine.
mkdir ("/var/mont/remote/");
$cmd = "sshfs -o password_stdin -o allow_other enzo#192.168.0.29:/home/enzo/remote/ /var/mont/remote/ <<< 'MyRemotePassword'";
$output = nl2br(shell_exec($cmd));
echo $output;
This script (test.sh) should work :
#!/usr/bin/env bash
php -r 'mkdir ("/var/mont/remote");
$cmd = "sshfs -o password_stdin -o allow_other enzo#192.168.0.29:/home/enzo/remote/ /var/mont/remote/ <<< 'MyRemotePassword'";
$output = nl2br(shell_exec($cmd));
echo $output;'
Run it as bash test.sh

Launch python file from PHP on LINUX server

I tried many solutions but nothing it works :
echo '<pre>';
shell_exec("python /home/folder/python/mapfile_compress.py");
shell_exec("sudo -u wwwexec python escapeshellcommand(/home/folder/python/mapfile_compress.py) $uid");
shell_exec("sudo chmod +x /home/folder/python/mapfile_compress.py");
system("bash /home/folder/python/mapfile_compress.py");
passthru("bash /home/folder/python/mapfile_compress.py");
passthru("/home/folder/python/mapfile_compress.py");
exec("bash /home/folder/python/mapfile_compress.py");
echo '</pre>';
I launched indivdually them but in all cases, Firebug returned : '<pre>'
So I tried this code founded on Stack Overflow :
$command = escapeshellcmd('chmod +x /home/folder/python/mapfile_compress_test.py');
echo $command;
$output = shell_exec($command);
echo $output;
But firebug returned nothing.
My python file begin with #!/usr/bin/env python and if I launch it on server that works !
Do you knwo how can I launch my python file from PHP file ?
chmod will return 0 on success and > 0 on error.
Make sure that the file is able to run by just executing it as the web user. When +x is properly set, you can execute it by just calling $ /path/to/your/file.py, the shebang in the first line in your script #!/usr/bin/env python should define the correct python based on your env.
You can test this by running:
$ /usr/bin/env python /path/to/your/file.py
So check your file permissions to check if the file is executable by the user that runs the php script.
Just to test, you can just print a few lines in your python file
#!/usr/bin/env python
print "test line 1"
print "test line 2"
Then if you have verified permissions and the correct use of python, you can do this in your php.
$command = escapeshellcmd('/path/to/your/file.py');
$output = shell_exec($command); // get all output or use passthrough, exec will only return the last line.
echo "<pre>{$output}</pre>;
At first, Do you have enabled shell_exec/system/passthru commands in php.ini?
shell_exec("python /home/folder/python/mapfile_compress.py");
I think, it could be problem with your $PATH. Try something like: (use full path to python)
shell_exec("/usr/bin/python /home/folder/python/mapfile_compress.py");
shell_exec("/usr/local/bin/python /home/folder/python/mapfile_compress.py");
In my case that's work if I write this code :
$command = escapeshellcmd('python /path/to/your/file.py');
exec($command);

Unoconv not creating pdf file in php script

When I run this phpscript in my mac xampp it return successful but not pdf file is generated. But when I run from mac terminal it able to create the pdf file from the docx file.
<?php
$source_file = 'c++ documentation.docx';
$cmd = "unoconv/0.7/bin/unoconv";
$command = sprintf("/Applications/XAMPP/xamppfiles/htdocs/phpdocx/unoconv/0.7/bin/unoconv -h -v -f pdf -o /phpdocx/docimages/testing.pdf %s 2>&1",
escapeshellarg($source_file));
echo shell_exec($command);
echo "yoyo";
if(shell_exec($command)){
echo "successful";
}else{
echo "failed";
}
?>
This is my edited code and it said sprintf(): Too few argument!
You need to escape the shell arguments, otherwise the shell may interpret its special characters:
$source_file = 'c++ documentation.docx';
$command = sprintf("unoconv/0.7/bin/unoconv -h -v -f pdf -o /phpdocx/docimages/testing.pdf %s 2>&1",
escapeshellarg($source_file));
Provide full (absolute) path to your unoconv/0.7/bin/unoconv executable, as it seems unavailable from your $PATH environment variable:
$command = sprintf("/path/to/unoconv/0.7/bin/unoconv -h -v -f pdf -o /phpdocx/docimages/testing.pdf %s 2>&1",
escapeshellarg($source_file));
Alternatively, include the absolute path to unoconv/0.7/bin/ directory into the $PATH environment variable before calling the shell. Refer to this post, for instance.
Obviously, the $source_file should be in the current directory. Otherwise, the command will fail to access it.

PHP shell tar problem, return code 2

Want to archive a folder using tar from PHP:
$result = shell_exec("tar cf $sourceFile $sourceFolder -C $source > /dev/null; echo $?");
var_dump($result);
Output:
string(2) "2 "
the > /dev/null; echo $? thing is for outputing the result code of a script under linux;
the -C $source - changes to the right folder before doing anything
This is really strange because when i run this from linux console, it works just fine - creates the archive, so it's not a permission issue.
Other sripts like "whoami" or "ls" work fine.
Any ideas what does it mean?
Maybe: shell_exec("/bin/bash tar ....")
Just for debugging purposes redirect stderr to stdout and use passthru() to display the complete output (possibly including error messages).
$cmd = sprintf("tar cf %s %s -C %s 2>&1",
escapeshellarg($sourceFile),
escapeshellarg($sourceFolder),
escapeshellarg($source)
);
echo '<pre>', htmlspecialchars($cmd), ": \n";
flush();
passthru($cmd, $code);
var_dump($code);
echo "</pre>";

Categories