Why can't PHP script write a file on server 2008 via command line or task scheduler?
I created a question on serverfault.com and was prompted to ask here.
I have a PHP script. It works well when I use a browser. It writes an XML file to the same directory. The script takes ~ 60 seconds to run and the resulting XML file is ~ 16MB.
I am running PHP 5.2.13 via FastCGI on Windows Server Web Edition SP1 64 bit.
The code pulls inventory from the SQL server, runs a loop to generate an XML file for a third party.
I created a task in the task scheduler to run c: \ php5 \ php.exe "D: \ inetpub \ tools \ build.php"
The Task Scheduler shows a time span of about a minute, namely how long the script takes to run in the browser.
No error was returned, but the file was not created.
Every time I make changes to the properties of the scheduled task, the user password window appears and I entered the password of the administrator account.
If I run this same path and argument on the command line, it is not an error and does not create a file.
When right clicked run Command Prompt as Administrator, file still not created. I get a "file posted" echo after the file and no error is returned.
I am doing a simple fopen fwrite fclose to store the content of a php variable in an XML file and the file is only created when the script is run through the browser.
This is what happens after the xml-building cycle:
$feedContent .= "</feed>";
sqlsrv_close( $conn );
echo "<p>feed built</p>";
$feedFile = "feed.xml";
$handler = fopen($feedFile, 'w');
fwrite( $handler, $feedContent );
fclose( $handler );
echo "<p>file published</p>";
code>
thanks
a source to share
Most likely, you need to provide the absolute path to the feed file. It's not clear where exactly you are executing the script, but the script's working directory "is usually where it was started:
c:\some\deeply\nested\directory> c:\php5\php.exe d:\inetpub\tools\build.php
will try to write the feed file to:
c:\some\deeply\nested\directory\feed.xml
not in c: \ php5 or d: \ inetpub \ tools
I don't know what the Task Scheduler uses as its default directory, but most likely this is not what you expect.
Also check that the account running the task in Task Scheduler has write access to the output directory. And always check if the called fopen()
name succeeded (it returns FALSE on error).
a source to share