Passing arguments dynamically to a batch file by using php - php

Here is my batch file script. I would like to pass arguments dynamically to the batch file. Based on the file name as part of the query string, it should copy that specific file to the network folder.
Echo off
set arg1=%1
net use \\windows_servername\test_folder shared_folder_password /user:shared_folder_username
xcopy C:\Apache\htdocs\Invoices\invoice_feed\%arg1% \\windows_servername\\test_folder
my PHP file code is below
echo $fname = $_GET['FILE_NAME'];
system("cmd /c C:\\scripts\batch1.bat ".$fname);
I am getting out put as The command was successfully completed. But file was not copied to the network folder.
But It is working fine, when I gave the file name directly on batch file like below.
xcopy C:\Apache\htdocs\Invoices\invoice_feed\sample.csv \\windows_servername\\test_folder
Any help would be greatly appreciated.

Related

Execute PHPSpreadsheet via batch script / task scheduler

I have created two php files, the perform two separate tasks.
1. The first script uses PHPSpreadsheet to create an populated my report template which is an xlsx file. This works perfectly when run directly through the browser (Google Chrome).
2. The second script uses PHPMailer to then pickup this file and attach it to an e-mail. Again this works perfectly when executed through the browser.
I have tried running the same scripts using a batch file, but this is where the first section fails. The Xlsx file never gets created, and hence an e-mail gets sent without attaching the document.
I have tried running each script individually via the batch script.
The second part (sending the mail) works as expected.
Creating the file via the batch script seems to be the issue.
REM This adds the folder containing php.exe to the path
PATH=%PATH%;C:\php
REM Change Directory to the folder containing your script
CD C:\Apache24\htdocs\phpspreadsheet\reporting
REM Execute
php report_v1.1.php
REM Change Directory to the folder containing your script
CD C:\Apache24\htdocs\Automation
REM Execute
php report.php
I would like to automate these two scripts to run sequentially, so that I can automate sending out the report to the relevant recipients. I do not get any error message from the first script, only a lot of random characters on the terminal screen when executing it via command line to test.
Updating the last part of my code to read
$writer->save(php_sapi_name() === 'cli' ? 'report_name.xlsx' : 'php://output');
did the trick. Thanks to #Hunman for the suggested solutions.

Python file will not run from php

I have a PHP file on my web server that is supposed to call a python script (same folder) that writes out to a file and then get its contents and displays the data.
The PHP file is called q.php
it contains
<?php
$tmp = exec("trivia.py");
sleep(4);
$homepage = file_get_contents('./testfile.txt', true);
echo $homepage + '<p>exec ret:' + $tmp;
echo exec("whoami");
?>
This file calls a trivia.py which writes out a file ("./testfile.txt") and then php gets the data from the file and displays it. I added a variable to see if the exec is working and it returns 0. The PHP server is being executed by user http.
now for trivia.py, I have the following line at the top of the file
#!/usr/bin/env python
and it executes perfectly fine when I SSH into the server. From SSH I run the script and it creates the file specified above and the web page works fine. However, if I used the PHP file to create it, it will not work from the web.
I am pretty sure this has something to do with permissions somewhere but I am not that great on permissions for Linux.
SYSTEM INFO: Synology Diskstation, DSM5, PHP5, Python 2.7
EDIT:
trivia.py currenlty has 777 as permissions with admin owner and group users
When running a file via exec you need to provide the full path to the file, it doesn't matter if the file is in the same directory.
Please try it with (assuming the file is in /var/www/public)
exec('/var/www/public/trivia.py');
For Windows User -
Problem was resolved here PHP exec python
I suggest to replace $tmp = exec("trivia.py"); for this one
$tmp = exec("C:\\Python27\\python.exe trivia.py");
// Add your python.exe route before the file
Hope it works!!

Batch script to download and save XLS file to directory

I am trying to write some batch script to download and save an XLS file from a URL. I am able to down load the file by using
#ECHO OFF
start /d "C:\Program Files\Internet Explorer" IEXPLORE.EXE url link
exit
I would now like to save these files to a folder or directory.
Any help anyone could provide here would be greatly appreciated.
There are at least two ways to do it.
Like Buddy suggested, use a command-line downloader, for example wget.
Under Linux, you can run PHP directly (even without a webserver). See PHP: Command Line PHP on Microsoft Windows in PHP manual.
Don't run a browser, or - even worse - IE, just to run a PHP script.

File not created (Linux-PHP-C++)

I am trying to save a file to the current directory in Linux and then display it on a webpage. Currently I run a C++ executable from a php script with the following code
exec("/var/www/html/radsim/plotFluence $rMin $rMax $zMin $zMax $lum $graphStyle $basepath[$path]", $return);
When I run the executable from the console in Linux the file is created fine, the problem arises when I try from within the php; the file is simply not in the directory . The user inputs values and the executable is run but no file is made. The C++ looks like this
canvas->Print(("/var/www/html/radsim/"+histoName+_FileFormat).c_str());
The permisions are set to 777. In addition, on another PHP script, I use fopen("data.txt", 'w') or die() to create a text file, but it always dies.
Seems like a sandbox. There must be a php config option - best to start here: http://php.net/manual/en/configuration.changes.php

Windows Batch File To Execute PHP Accessing mySQLi Database

I am writing a windows batch file that needs to execute a PHP file which fetches data from a backend and inserts into mysql database.
Below is the code I used and it is working but it will open the browser.
#ECHO OFF
START http://localhost/test.php
How do I ensure that the browser is not invoked when START is executed? I have tried to put /B at the back but it not working.
I have also tried the following but it is not working at all and nothing gets inserted.
#ECHO OFF
php.exe -f "C:\wamp\www\test.php"
The simple answer is that php files are set up to open with your default browser, when you open the php file, it will open with that browser.
If you want to view the contents of the file in cmd instead you can use
type test.php

Categories