使用 cron 在 PHP 中编写文件

发布于 2024-07-27 16:23:59 字数 1147 浏览 1 评论 0原文

我正在尝试使用 file_ put_contents 将文件写入 cron 脚本所在目录的子文件夹中。 但是,我不断收到警告“无法打开流:没有这样的文件或目录。” 我有这个目录结构:

httpdocs/scripts/fileDirectory

cron 脚本位于脚本文件夹中。 我用 cron 命令调用它:

php httpdocs/scripts/cron_writeFile.php

在 cron_writeFile 文件中,我首先尝试:

file_put_contents('fileDirectory/', $fileName, $fileContents);

当我在浏览器中加载页面时,它会起作用,但在 cron 执行时不起作用。

当我在 cron 中 require_once 一个文件时,我必须将“绝对”路径放入它:

require_once('httpdocs/scripts/requiredFile.php');

所以,我尝试了:

file_put_contents('httpdocs/scripts/fileDirectory/', $fileName, $fileContents);

不走运。 我很确定它会进入正确的文件夹,因为警告是:

“警告:file_put_内容(httpdocs/scripts/fileDirectory/4.txt): 无法打开流:没有这样的文件或目录 /var/www/vhosts/myDomain.com/httpdocs/scripts/cron_writeFile.php 第 93 行”

这两个目录都有写权限。

我正在使用正在运行的 VPS(我知道它很糟糕,我需要升级,但我没有权限)
Parallels Plesk 面板版本 9.2.1 PHP 5.0.4

文件不存在,每次脚本运行时我都需要一个新文件。
我不确定是否有某种方法来定义文件路径或我缺少的其他东西。

感谢您的帮助!

I am trying to write a file to a sub folder of the directory my cron script is in using file_ put_ contents. However, I keep getting a warning "failed to open stream: No such file or directory."
I have this directory structure:

httpdocs/scripts/fileDirectory

The cron script lives in the scripts folder. I call it with the cron command:

php httpdocs/scripts/cron_writeFile.php

In the cron_writeFile file, I first tried:

file_put_contents('fileDirectory/', $fileName, $fileContents);

which works when I load the page in a browser, but not when the cron executes.

When I require_once a file in a cron, I have to put the 'absolute' path to it:

require_once('httpdocs/scripts/requiredFile.php');

So, I tried that:

file_put_contents('httpdocs/scripts/fileDirectory/', $fileName, $fileContents);

No luck. I'm pretty sure it's getting to the right folder because the warning is:

"Warning: file_ put_ contents(httpdocs/scripts/fileDirectory/4.txt):
failed to open stream: No such file or directory in
/var/www/vhosts/myDomain.com/httpdocs/scripts/cron_writeFile.php
on line 93"

The both directories have write permissions.

I am using a VPS running (I know it sucks and I need to upgrade, but I don't have the authority)
Parallels Plesk Panel version 9.2.1
with PHP 5.0.4

The file does not exist and I need a new file each time the script runs.
I am not sure if there is a certain way to define the file path or some other thing I am missing.

Thanks for your help!

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

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

发布评论

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

评论(1

勿忘初心 2024-08-03 16:23:59

如果您使用 dirname(__FILE__) (see) 来获取您的目录的绝对路径,该怎么办? “文件目录”目录?

我想,如果您的 fileDirectory 是文件所在目录的子目录,您的脚本中会出现类似的情况:

file_put_contents(dirname(__FILE__) . '/fileDirectory/' . $fileName, $fileContents);

这样,您就不会依赖相对路径,如果您的脚本可能会出错不是从“正确的”目录启动的。

What if you use dirname(__FILE__) (see) to get an absolute path to your "fileDirectory" directory ?

Something like this in your script, I guess, if your fileDirectory is a child of the directory where your file resides :

file_put_contents(dirname(__FILE__) . '/fileDirectory/' . $fileName, $fileContents);

That way, you don't depend on relative path, which can be wrong if your script is not launched from the "right" directory.

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