【问题标题】:Writing a file in PHP with a cron使用 cron 在 PHP 中编写文件
【发布时间】:2009-07-16 21:37:24
【问题描述】:

我正在尝试使用 file_put_ 内容将文件写入我的 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);

运气不好。我很确定它会到达正确的文件夹,因为警告是:

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

两个目录都有写权限。

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

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

感谢您的帮助!

【问题讨论】:

    标签: php cron plesk


    【解决方案1】:

    如果您使用dirname(__FILE__) (see) 获取“fileDirectory”目录的绝对路径会怎样?

    如果你的 fileDirectory 是你的文件所在目录的子目录,我猜你的脚本中有这样的内容:

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

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

    【讨论】:

      猜你喜欢
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      相关资源
      最近更新 更多