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).

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

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?