Passing values to a windows batch file through PHP - 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"?

Related

Unable to get output from gradle when executing from PHP-script exec()

I am trying to build an Android application by executing Gradle from a PHP script using exec(), but there is no response, so I am unable to see why the build fails.
When executing the applications gradle wrapper from the terminal (SSH), the whole process works fine, using the following commands:
x:/path/to/project/$ ./gradlew assembleRelease
x:/path/to/project/$ ./gradlew assembleRelease --debug --stacktrace
x:/path/to/project/$ ./gradlew assembleRelease --debug --stacktrace > output.txt
The output works fine and I am able to troubleshoot any possible problems. When executing gradle from a PHP-script, there is no output.
// array for the output to be dumped into
$output_array = array();
// $result is empty (tried with stacetrace, debug and an output file)
$result = exec("cd /path/to/project && ./gradlew assembleRelease", $output_array);
// nothing displayed
var_dump($output_array);
There is no way of me telling where the problem is. I have checked that the required environment variables ($GRADLE_HOME and $ANDROID_HOME) are set using getenv() and they point to the correct path.
Any help would be great!
I'm no linux specialist, but it seems you launch it (ssh) from "~/path/to/project" (~ meaning your home path).
In php, you're trying to launch it from "/path/to/project" (absolute path from root folder).
try to replace your second command by "ls" so you'll see where you are... and compare with where you should be!

PHP Execute bat file on windows server

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?

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 ?

Generating pdf from webpage in php

how can I execute command wkhtmltopdf http://google.com /tmp/test.pdf from server ie http://localhost/test.php, when I do it from command line it works. I tried system() and exec() functions but did not work. When I use system('touch /tmp/test') file is created. What stops wkhtmltopdf? Is it php, apache?
Make sure that the user the script is running as knows where wkhtmltopdf bin is.
You can find out where it is with the which command.
which wkhtmltopdf
Also you can get the return status of a command by setting a variable equal to it
e.g.
$last_line = system('ls');
echo $last_line

How to run "ANT" command in batch file through PHP?

My batch file includes following code,
cd C:\Ant
Ant
How can I execute this file in PHP.
I've tried everything including,
system();
exec();
passthru();
but none of these functions worked for me.
Batch files are no executables. Try
cmd /c your-batch-file.bat
Here cmd.exe is the executable. The /c options tells cmd to run the following command (here your batch file).
Update:
Without any PHP knowledge: the functions you are trying to use might be disabled due to safe mode.
It would be helpful (for any PHP developers) if you post the code line that tries to call the bat. Maybe you just missed a parameter or something like that.

Categories