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.
Related
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
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);
I am doing the conversion from youtube link to gif image ,but i faced some problem while executing exce() function.
echo $ret = exec("youtube-to-gif -u https://www.youtube.com/watch?v={$vidID} -b $start_second -d $different_second -s 480x? -o {$filePath}{$fileName} -f 10", $out, $err);
I am using exec() ,but its not returning any value .Here i am not getting why it is not working.
Thanks ,any suggestion will highly appreciate.
First, store your command in a variable and try echoing it and runnining in a terminal to see if it's valid at all:
$command = "youtube-to-gif -u https://www.youtube.com/watch?v={$vidID} -b $start_second -d $different_second -s 480x? -o {$filePath}{$fileName} -f 10";
echo $command . PHP_EOL;
echo $ret = exec($command, $out, $err) . PHP_EOL;
If it works fine when you run it manually, try full path to youtube-to-gif. Assuming you are running php on Linux, you should be able to do it with this command:
which youtube-to-gif
Now replace youtube-to-gif with the full path in $command.
This works in the command prompt(in one line) :
cd C:\Program Files (x86)\LibreOffice 4\program &&
"C:\Program Files (x86)\LibreOffice 4\program\python.exe" unoconv -f pdf d:\class1.doc
but when it comes to do the same in PHP's exec() nothing happens - neither message nor any file, probably due to a syntax error :
echo exec('cd C:\\Program Files (x86)\\LibreOffice 4\\program &&
"C:\\Program Files (x86)\\LibreOffice 4\\program\\python.exe" unoconv -f pdf d:\\class1.doc');
Once you have cd into a dir, no need to call the executable within it from the full path again. you can call it directly.
Try this:
<?php
// Test
$ret = exec('cd "C:\Program Files (x86)\LibreOffice 4\program\" && python.exe unoconv -f pdf d:\\class1.doc', $output, $error);
// Debug
var_dump($ret);
var_dump($output);
var_dump($error);
?>
Take a look at the documentation here: http://us1.php.net/function.exec
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