【问题标题】:unlink not working when php script is ran by cron当 cron 运行 php 脚本时,取消链接不起作用
【发布时间】:2018-05-11 02:14:41
【问题描述】:

我有一个用于检查病毒总扫描结果的 php 脚本。如果扫描返回正面的恶意代码,它会将 db 中的值更改为 0。我有另一个检查值的 php 脚本,如果它是 0,它会从数据库中删除条目,然后从目录中删除文件。当我通过命令行运行它时,它运行良好,但是当 cron 运行它时,它确实删除了 db 条目,但它不会从目录中删除文件。

任何帮助将不胜感激。

这里是取消链接的 php 文件的结尾:

else{
        // if not it deletes the image
        $hash = $row['hash'];
        $result2 = mysqli_query($connection, "DELETE FROM userUploads WHERE hash = '$hash' ");

        // need due to dir structure
        $pat = str_replace('../','', $row['fileName']);
        unlink ($pat);

        if (! $result2){
            echo('Database error: ' . mysqli_error($connection));

        }

供参考,这里是完整的文件:

<?php

function VirusTotalCheckHashOfSubmitedFile($hash){
    $debug = false;
    $post = array('apikey' => '','resource'=>$hash);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.virustotal.com/vtapi/v2/file/report');
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); // please compress data
    curl_setopt($ch, CURLOPT_USERAGENT, "gzip, My php curl client");
    curl_setopt($ch, CURLOPT_VERBOSE, 1); // remove this if your not debugging
    curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $result = curl_exec ($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($status_code == 200) { // OK
      $js = json_decode($result, true);
       if($debug){echo "<pre>";}
       if($debug){print_r($js);}
      if($js["positives"]==0){
          // clean
        return true;
      }else{
          // malware
        return false;
      }
    } else {  // Error occured
      print($result);
    }
    curl_close ($ch);
}


$connection = mysqli_connect("");
if (!$connection) {
    trigger_error("Could not reach database!<br/>");

}

$db_selected = mysqli_select_db($connection, "seclog");
if (!$db_selected) {
    trigger_error("Could not reach database!<br/>");

} 
// Selecs images that have not been marked as clean by virus total
$result = mysqli_query($connection, "Select hash, fileName FROM userUploads WHERE scanResult = 0"); 
if (! $result){
    echo('Database error: ' . mysqli_error($connection));

}
while ($row = mysqli_fetch_assoc($result)) {
    // checks for results on scanned images
    if(VirusTotalCheckHashOfSubmitedFile($row['hash'])){
        // if report returns image is malware free we update its virusFree attribute to true
        $hash = $row['hash'];
        $result2 = mysqli_query($connection, "UPDATE userUploads SET scanResult = 1 WHERE hash = '$hash'"); 
        if (! $result2){
            echo('Database error: ' . mysqli_error($connection));

        }
    }else{
        // if not it deletes the image
        $hash = $row['hash'];
        $result2 = mysqli_query($connection, "DELETE FROM userUploads WHERE hash = '$hash' ");

        // need due to dir structure
        $pat = str_replace('../','', $row['fileName']);
        unlink ($pat);

        if (! $result2){
            echo('Database error: ' . mysqli_error($connection));

        }
    }
}  

?>

【问题讨论】:

  • 可能是cron用户的文件权限问题。被删除的文件必须是用户可写的:运行 php cron 的组。
  • @AlexBarker 我尝试通过 sudo crontab 运行它,它有 rw 但没有乐趣。
  • 使用 root 或 /etc crontab。检查您的 php 日志以了解其失败的原因,您的用户可能没有 sudo 访问您的脚本的权限。
  • 我已经尝试过 root crontab。 cron 日志在脚本执行时不会显示任何错误,只是忽略了取消链接。

标签: php linux cron unlink


【解决方案1】:

问题几乎可以肯定是路径$pat = str_replace('../','', $row['fileName']);。 Crons 执行 PHP cli,这与 Apache 执行的 PHP 不同,也是另一个上下文。尝试设置绝对路径:

$pat = "/var/www/myfolder/myscript.some";

如果由于某种原因您需要一个变量,因为文件夹结构取决于上下文(例如开发、生产),您可以在 cron 执行 PHP 时将该变量作为参数传递:

//this is the cron
30 17 * * 1 myscript.php myvar

myscript.php 中的$argv[1]myvar

【讨论】:

  • 你是 100% 正确的,非常感谢你的帮助,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
  • 2011-05-10
  • 2018-07-30
  • 2011-01-07
  • 2019-04-24
  • 1970-01-01
相关资源
最近更新 更多