我正在尝试使用 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!
发布评论
评论(1)
如果您使用
dirname(__FILE__)
(see) 来获取您的目录的绝对路径,该怎么办? “文件目录”目录?我想,如果您的
fileDirectory
是文件所在目录的子目录,您的脚本中会出现类似的情况:这样,您就不会依赖相对路径,如果您的脚本可能会出错不是从“正确的”目录启动的。
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 :That way, you don't depend on relative path, which can be wrong if your script is not launched from the "right" directory.