当 php 脚本为 运行 时取消链接不起作用

unlink not working when php script is ran by cron

我有一个 php 脚本来检查 virustotal 扫描的结果。如果扫描 returns 对恶意代码呈阳性,它会将数据库中的值更改为 0。我有另一个 php 脚本来检查值,如果它是 0,它会从数据库中删除条目,然后从目录中删除文件。当我 运行 通过命令行运行它时它完美地工作,但是当 cron 运行s 它时,它确实删除了 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));

        }
    }
}  

?>

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

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

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

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

里面 myscript.php $argv[1]myvar.