So I am uploading file via
<form action="" method="post" enctype="multipart/form-data">
.
After upload I need to send the file to virustotal API for scanning.
But I get the error:
{ "error": { "message": "Received file with an empty filename. Please post with filename.", "code": "BadRequestError" } }
Here is my code. I don't understand how I am posting the file incorrectly:
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. File is uploading successfully on server.
CodePudding user response:
using @ to upload files with curl was deprecated in 5.5, stopped working by default in 5.6, and was completely removed in 7.0
either downgrade to PHP <=5.5, or replace
$post['file'] = '@'.$file_to_scan;
with
$post['file'] = new CURLFile($file_to_scan);
also get rid of
'Content-Type: multipart/form-data'
- let libcurl generate it for you, the actual header is supposed to look like
Content-Type: multipart/form-data; boundary=------------------------718e1fbbd5629679
- with a boundary generated by curl.