VirusTotal API 通过 PHP CURL 给出错误 'empty filename'

VirusTotal API via PHP CURL gives error 'empty filename'

所以我正在通过

上传文件

<form action="" method="post" enctype="multipart/form-data">.

上传后我需要将文件发送到virustotal API进行扫描。

但我收到错误消息:

{ "error": { "message": "Received file with an empty filename. Please post with filename.", "code": "BadRequestError" } }

这是我的代码。我不明白我是如何错误地发布文件的:

if(isset($_POST["submit"])) {
    // enter here the path of the file to be scanned
    $target_dir = "files/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    $file_to_scan = "https://webbasedapplication.dreamhosters.com/" . $target_file;
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        echo "The file ". htmlspecialchars( basename( $_FILES["file"]["name"])). " has been uploaded.<hr>";
    } else {
        echo "Sorry, there was an error uploading your file.<hr>";
    }
    $post_url = 'https://www.virustotal.com/api/v3/files';
    $post['file'] = '@'.$file_to_scan;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$post_url);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $headers = [
        'x-apikey: 12345abcde',
        'Accept: application/json',
        'Content-Type: multipart/form-data'
    ];
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $api_reply = curl_exec($ch);
    print $api_reply;
}

P.S。文件正在服务器上成功上传。

使用@通过 curl 上传文件在 5.5 中已弃用,在 5.6 中默认停止工作,并在 7.0 中完全删除

要么降级到 PHP <=5.5,要么更换

    $post['file'] = '@'.$file_to_scan;

    $post['file'] = new CURLFile($file_to_scan);

也去掉

        'Content-Type: multipart/form-data'
  • 让 libcurl 为您生成它,实际的 header 应该看起来像
Content-Type: multipart/form-data; boundary=------------------------718e1fbbd5629679
  • 带有由 curl 生成的边界。