Question

I created a question on serverfault.com, and it was recommended that I ask here.

https://serverfault.com/questions/140669/why-cant-php-script-write-a-file-on-server-2008-via-command-line-or-task-schedul

I have a PHP script. It runs well when I use a browser. It writes an XML file in the same directory. The script takes ~60 seconds to run, and the resulting XML file is ~16 MB.

I am running PHP 5.2.13 via FastCGI on Windows Server Web edition SP1 64 bit.

The code pulls inventory from SQL server, runs a loop to build an XML file for a third party.

I created a task in task scheduler to run c:\php5\php.exe "D:\inetpub\tools\build.php"

The task scheduler shows a time lapse of about a minute, which is how long the script takes to run in a browser.

No error returned, but no file created.

Each time I make a change to the scheduled task properties, a user password box comes up and I enter the administrator account password.

If I run this same path and argument at a command line it does not error and does not create the file.

When I right click run command prompt as an administrator, the file is still not created. I get my echo statement "file published" that is after the file creation and no error is returned.

I am doing a simple fopen fwrite fclose to save the contents of a php variable to a .xml file, and the file only gets created when the script is run through the browser.

Here's what happens after the xml-building loop:

$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>";

Thanks

Was it helpful?

Solution

Most likely you need to specify an absolute path to your feed file. It's not clear exactly where you're executing the script from, but the script's "working directory" is generally where it was started from:

 c:\some\deeply\nested\directory> c:\php5\php.exe d:\inetpub\tools\build.php

will be trying to write the feed file in:

 c:\some\deeply\nested\directory\feed.xml

and not in c:\php5 or d:\inetpub\tools

I don't know what the task scheduler uses as its default directory, but most likely it's not what you're expecting.

Also check that the account you're running the job under in the task scheduler has write permissions on the output directory. And always check if an fopen() called succeeded (it returns FALSE on failure).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top