【问题标题】:php function to check when an uploaded image has been in the directory for longer than a dayphp 函数检查上传的图像何时在目录中超过一天
【发布时间】:2015-06-01 18:57:56
【问题描述】:

我有一个 cron 控制器类,它基本上处理所有与 cron 作业相关的工作。我正在尝试向控制器添加一个功能,以删除目录中超过一天的图像。这是我到目前为止所拥有的,我认为这对我来说很有意义,但是当我测试它以查看它是否有效时,我会收到以下错误:

警告:filemtime(): stat failed for 4148_1432931936_0.jpeg

警告:取消链接(public/images/gallery_images/files/4148_1432931936_0.jpeg

public function delete_gallery_images()
{
    $dir = opendir('../public/images/gallery_images/files/');

    if ($dir) {
            // Read directory contents
        while (false !== ($file = readdir($dir))) {

            if($file != "." && $file != "..") {
                // Check the create time of each file (older than 1 day)
                if (filemtime($file) < (time() - 60 * 60 * 24)) {
                    unlink('../public/images/gallery_images/files/'.$file);
                }
            }
        }
    }
//      //close dir
//      closedir($dir);
}

有什么想法吗?非常感谢你。

【问题讨论】:

  • public 通常是服务器系统路径的一部分,而不是实际的文件夹获取方法,例如相对路径。
  • 对不起,我现在在本地工作,这个使用 WAMP
  • 您使用的是../public/,但随后使用的是unlink('public/ - 当使用服务器系统路径时,通常是/public/...
  • 啊,你是对的!!很好的收获

标签: php file-upload controller cron unlink


【解决方案1】:

看起来您的目录名称不一致。试试这个:

public function delete_gallery_images()
{
    $dirName = '../public/images/gallery_images/files/';
    $dir = opendir($dirName);

    if ($dir) {
            // Read directory contents
        while (false !== ($file = readdir($dir))) {
            if($file != "." && $file != "..") {
                // Check the create time of each file (older than 1 day)
                $fname = $dirName . $file;
                if (filemtime($fname) < (time() - 60 * 60 * 24)) {
                    unlink($fname);
                }
            }
        }
    }
    //close dir
    closedir($dir);
}

【讨论】:

    猜你喜欢
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多