PHP Execute bat file on windows server - php

I have a PHP file that creates a pdf fill form and uses pdftk to merge the data into the template.
I am having trouble getting php to execute the batch file in order to run the program and merge it.
$current = '\\oma-entfs-002\aps\wwwroot\tuition\uploads\';
My PHP code:
$WshShell = new COM("WScript.Shell");
$WshShell->exec($current.'makePDF.bat ' .$fdf_file.' '.$newPDF);
The Batch File:
pushd \\oma-entfs-004\APS\wwwroot\tuition
pdftk uploads/Educational_Assistance_Request_Form_North_America.pdf fill_form uploads/%1 output uploads/%2 need_appearances
popd
Both COM and Exec are enabled on the server as far as I can tell.
When I run the batch file from command line, it works just fine so I think there is something with PHP not running the file correctly.
Any suggestions on the best way to debug this and identify the root cause?

Related

Generate android apk using shell script from php?

I'm trying to generate Android apk files from shell script, I want to execute a shell script file from PHP. When I run the shell script in the terminal, it works perfectly. If I try to run the script using PHP, the shell script doesn't execute all the commands. The ls command in shell script works perfectly, but when executing using PHP, other commands doesn't work. I'm using xampp server in a Linux environment.
My shell script
cd /home/user/AndroidStudioProjects/msvep4247-inghamautogroup-pulse-and/
./gradlew assembleDebug
cp -fr app/build/outputs/apk/app-debug.apk /opt/lampp/htdocs/sample/apk
ls
Shell script ls output
app autolead_data_format.pdf build build.gradle cheek gradle
gradle.properties gradlew gradlew.bat lib local.properties
msvep4247-inghamautogroup-pulse-and.iml settings.gradle
My PHP script
<?php
echo shell_exec('ls');
echo shell_exec('./generateApk.sh');
?>
PHP script ls output
generateApk.sh generate.php APK
Note: ls outputs file names in the folder
I set all the file permissions for shell script in the xampp server. Can anyone describe where I'm mistaken? Awaiting responses...
Just use the full path to the script/executable, because the environment is different when running from php.
It seems that PATH environment variable in PHP code that is executed in the web server is more limited than the one in the shell you're working in. But you can change environment variables in PHP, and the commands you start from it will see those changes.
<?php
// set content type so the output is more readable in the browser
header('Content-Type: text/plain');
// set $PATH to some limited value
putenv('PATH=/bin:/sbin');
// verify, note that we have to use full path to 'env'
print(shell_exec("/usr/bin/env|grep '^PATH='"));
// this command won't run (assuming its full path is /usr/bin/id)
print(shell_exec("id"));
// add more directories to $PATH
putenv('PATH=/bin:/sbin:/usr/bin:/usr/sbin');
// verify again, we can use env without specifying the path this time
print(shell_exec("env|grep '^PATH='"));
// this command will
print(shell_exec("id"));
So you have to write putenv('PATH=<your_shell_PATH_contents>'); at the top of your PHP script. Using full path to the shell script alone won't help if the script itself uses relative paths to binaries it starts.

Try import CSV to MongoDB with PHP shell_exec() but seems not work

I am trying to import a CSV to server's mongoDB by php. I already done the update file part and now I can get the CSV file that client update to the server. so now I am trying to import CSV file to mongoDB by using shell_exec() , here is my PHP code.
$importCommand = 'mongoimport -d test -c test --type csv --file yourfile.csv --headerline';
$output = shell_exec($importCommand.' 2>&1');
echo $output;
when I run this part of code in terminal by php -f it works(I know my CSV is not there so no import has been made),
$ mongoimport -d test -c test --type csv --file yourfile.csv --headerline
2017-03-22T13:46:22.668+0800 Failed: open yourfile.csv: no such file or directory
2017-03-22T13:46:22.668+0800 imported 0 documents"
as can be seen this works perfectly fine. The 'mongoimport' can be found and work fine.
However when I add this to my php that use to update the file the output become
sh: mongoimport: command not found
Any idea how to solve this problem? Thanks for your time.
I think I know, its due to my webserver process won't necessarily be set up with the same configuration as your own account.
MongoDB's bin is for me,
but apache user is '_www'.
so I need to fix this privilege to get my PHP run mongoimport via safari.
or input /usr/local/mongodb/bin/mongoimport to get it done
Thanks StackOF I can answer myself by just posting here

Passing values to a windows batch file through PHP

I am trying to get a batch file to run with commands that I am passing it. For security reasons we execute our commandline calls through WScript.Shell
This is what my batch file looks like:
#echo off
pushd \\theServer\wwwroot\tuition\uploads\
pdftk PDF_Template.pdf fill_form %1 output %2 need_appearances
popd
I am then passing the vars to the bat file like so:
$WshShell = new COM("WScript.Shell");
$WshShell->exec(getcwd().'\uploads\test.bat 1412194760.fdf output.pdf');
When I run the bat file from the server, everything works fine. However, not getting any results when it runs from php.
This example worked just fine when I had the full paths in there which I am trying to get rid of now using the code above.
$WshShell->exec('"pdftk" C:\\inetpub\\wwwroot\\tuition\\uploads\\Educational_Assistance_Request_Form_North_America.pdf fill_form C:\\inetpub\\wwwroot\\tuition\\uploads\\'.$fdf_file.' output C:\\inetpub\\wwwroot\\tuition\\uploads\\'.$newPDF.' need_appearances');
Can you see anything obvious I am doing wrong?
Update:
As a test I hard coded the values into the batch file:
pdftk Educational_Assistance_Request_Form_North_America.pdf fill_form 1412194760.fdf output output.pdf need_appearances
I then tried just running the file with both run and execute
$WshShell->run(getcwd().'\uploads\test.bat');
$WshShell->exec(getcwd().'\uploads\test.bat');
This test also failed.
Maybe its a permission error for shell running a batch file?
FIX:
So even though I was running the exec command in this location:
getcwd().'\uploads\test.bat
When I logged it, it was only running it in the cwd and ignored the uploads folder.
I changed the batch file to this and it now works fine:
pdftk uploads/Educational_Assistance_Request_Form_North_America.pdf fill_form uploads/%1 output uploads/%2 need_appearances
I would use the "run" command of the WshShell object instead. In any case, why do you have the pdftk command surrounded by double quotes"?

Php not executing system command

I have downloaded Tesseract OCR, installed it on windows and set its path variable and test it as well.
https://github.com/thiagoalessio/tesseract-ocr-for-php
I have downloaded its php script too and did some basic testing.
echo $this->buildTesseractCommand();
exec(trim($this->buildTesseractCommand()));
The command
echo tesseract C:\xampp\htdocs\OCR\test\images\hello.png C:\xampp\htdocs\OCR\test\temp\29847.txt
where hello.png is file to read characters from and 29847.txt is random file generated from cmd in which output is stored. although the same command is working directly through cmd.
But sadly the command is not working through php and no .txt file is generated , but when I paste the same in my command prompt, it works and file is generated.
I have tried system(), exec() and passthru() functions to run command but its not working in any :(
Any idea how to run it or any alternatives ?

Unable to execute ttf2afm from php

I am working on a project that involves PDF API TCPDF. So I needed an area in admin where site admin can upload and install new fonts to be used with TCPDF.
I am working on a script that does following :
1) upload TTF font to TCPDF fonts/utils/ directory.
2) execute ttf2afm from PHP script and create .AFM (adobe font metrics)
$command = escapeshellarg("/usr/bin/ttf2afm $fontPath$fontName -o $fontPath$afmName");
$result = passthru($command);
or
$command = escapeshellarg("ttf2afm $fontPath$fontName -o $fontPath$afmName");
$result = passthru($command);
3) execute php -f makefont.php font.ttf font.afm and generate the required font.php and font.z files.
now my problem is, the above commands are not executing from web page. If I copy and execute part of this code from php interactive shell it works well. But, from webpage, it simply does not work...
Is there some permission related problem? or I can not execute such commands from a web page?
Thanks in advance
First, escapeshellarg is used wrong. Better is:
$command = escapeshellcmd("/usr/bin/ttf2afm")." ".escapeshellarg($fontPath.$fontName)." -o ".escapeshellarg($fontPath.$afmName);
Also make sure that error logging is enabled, so you can see if there is a permission error.

Categories