所以我通過上傳檔案
<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;
}
PS 檔案正在服務器上成功上傳。
uj5u.com熱心網友回復:
使用 @ 上傳帶有 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 為您生成它,實際的標題應該看起來像
Content-Type: multipart/form-data; boundary=------------------------718e1fbbd5629679
- 帶有由 curl 生成的邊界。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/345494.html
