Execute PHPSpreadsheet via batch script / task scheduler - php

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.

Related

PHP prevent file caching in script

I am using php scripts as cron jobs.
I have this script, which opens a CSV file , parse its content to an array, then modify it and close it.
I added this script to the crontab.
The problem is when I delete that CSV file manually from the filesystem, the script still finds it and parse the old content as if that file was somehow cached.
When I execute the script using the command line, it works properly (does not find the CSV file).
I added at the beginning of the script clearstatcache() then opcache_reset() but nothing works
Thank you for your help

how to know that a new file is added in a folder using php

I have a folder called pdf where user will upload pdf's in it,i want a php script which will run for every five minutes to read the pdf folder in order to check for the newly added files in that folder?and intimate to the admin that few files with the following file names are added,how can i do that using php
php scripts cannot auto execute it only execute if there is request made to them..
a simple idea in your case could pe
1.write a php function with the logic to see if filesystem has changed and do the necessary
action(email/database update) on execution.
place & call this function on your home page(index.php).

Run a php script via Windows Batch File - not working in Task Scheduler only

My .bat files do not work with Task Scheduler.
For testing purposes, I created an extremely simple, one line .bat file:
php E:\POC\route.php
{the router.php file contain some file operation}
If I run this file by double-clicking it, it works correctly and Returned The Expected Output
But if I create a task to run this very same file, the text file is NOT created. The History log for the task says that the task was started and completed successfully yet no text file is created.
Note: Already i set the environment variable for php ("E:\Php")
Any thoughts? Thanks.

How to create a shortcut to run PHP code in Windows?

I wrote some PHP to generate a single PDF file with multiple pages based on input from a Comma Seperated Values (.csv) file.
Someone who is not familiar with programming will be running the PHP very often to generate the PDF file. That person will be responsible for changing the CSV file before generating it.
I need to know how I can provide him/her a shortcut in Windows so that when he/she click it, the PHP should execute and produce the PDF file in a hardcoded path.
You can create an shortcut on file itself, or executive php bin. Check Command Line PHP on Microsoft Windows.
Just create shortcut with C:\PHP5\php.exe -f "C:\PHP Scripts\script.php" -- -arg1 -arg2 -arg3, but it will be depended on locations. So you could create association with php files and just create shortcut to your script.
Or you could launch web-server (php web-server) and put link to your script to favorites in browser.
Just create a shortcut to the page, e.g.
http://www.example.com/mypdf.php
You can even open the link with a specific browser by typing one of these as the shortcut location:
chrome http://www.example.com/mypdf.php
firefox http://www.example.com/mypdf.php
iexplore http://www.example.com/mypdf.php
Alternatively, if you're indeed running PHP as CLI-only, see #sectus' answer.

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