doufei6456 2010-05-11 15:36
浏览 31
已采纳

为什么PHP脚本不能通过命令行或任务调度程序在服务器2008上写入文件?

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

  • 写回答

1条回答 默认 最新

  • dqg2269 2010-05-11 17:19
    关注

    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
    ested\directory> c:\php5\php.exe d:\inetpub\tools\build.php
    

    will be trying to write the feed file in:

     c:\some\deeply
    ested\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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作