i am pretty new to php. I am trying to upload a file. And its working. But now I am trying to make a POST request via C# (does not depend on C#, the error must be somewhere in my php code) and its not working. The Response/Error says:
Undefined index: fileToUpload in <b>C:\xampp\htdocs\subdir\process.php</b> on line <b>8</
line 8 for example is $target_file = $target_dir . rand() .basename($_FILES["fileToUpload"]["name"]);
I checked the request header with fiddler when I upload the file successfully via the website but I am not getting any information that could help me.
Fiddler Raw Request Header:
POST /subdir/process.php HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 45551695
Cache-Control: max-age=0
Origin: http://localhost
Upgrade-Insecure-Requests: 1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryepwGfigXDyjxAtPu
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3650.1 Iron Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://localhost/subdir/upload.php
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Upload.php:
<form action="process.php" method="post" enctype="multipart/form-data">
<b>Select file to upload:</b><br><br>
<input type="file" name="fileToUpload" id="fileID"><br>
<input type="submit" value="Upload file" name="submit">
</form>
Process.php:
header("Content-type: text/plain");
$target_dir = "uploads/";
$target_file = $target_dir . rand() .basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.
Error Code:". $_FILES["fileToUpload"]['error'];
}