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

发布于 2024-09-01 07:43:04 字数 1277 浏览 3 评论 0原文

我在 serverfault.com 上创建了一个问题,建议我在这里提问。

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

我有一个PHP 脚本。当我使用浏览器时它运行良好。它在同一目录中写入一个 XML 文件。该脚本运行大约需要 60 秒,生成的 XML 文件大约为 16 MB。

我正在 Windows Server Web 版 SP1 64 位上通过 FastCGI 运行 PHP 5.2.13。

该代码从 SQL Server 中提取清单,运行一个循环来为第三方构建 XML 文件。

我在任务计划程序中创建了一个任务来运行 c:\php5\php.exe "D:\inetpub\tools\build.php"

任务计划程序显示大约一分钟的时间间隔,这是脚本运行所需的时间在浏览器中。

没有返回错误,但没有创建文件。

每次我更改计划任务属性时,都会出现一个用户密码框,我需要输入管理员帐户密码。

如果我在命令行运行相同的路径和参数,它不会出错并且不会创建文件。

当我右键单击以管理员身份运行命令提示符时,该文件仍然没有创建。我在文件创建之后收到回显语句“文件已发布”,并且没有返回错误。

我正在执行一个简单的 fopen fwrite fclose 将 php 变量的内容保存到 .xml 文件中,并且仅当脚本通过浏览器运行时才会创建该文件。

以下是 xml 构建循环之后发生的情况:

$feedContent .= "";

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

完美的未来在梦里 2024-09-08 07:43:04

您很可能需要指定 feed 文件的绝对路径。目前尚不清楚您从哪里执行脚本,但脚本的“工作目录”通常是它的启动位置:

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

将尝试将提要文件写入:

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

而不是 c:\php5 或 d:\inetpub \tools

我不知道任务计划程序使用什么作为其默认目录,但很可能它不是您所期望的。

另请检查您在任务计划程序中运行作业的帐户是否对输出目录具有写入权限。并且始终检查 fopen() 调用是否成功(失败时返回 FALSE)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文