首页 > 解决方案 > 通过发布请求传输文件

问题描述

我对php很陌生。我正在尝试上传文件。及其工作。但是现在我正在尝试通过 C# 发出 POST 请求(不依赖于 C#,错误必须在我的 php 代码中的某个地方)并且它不起作用。 响应/错误说:

Undefined index: fileToUpload in <b>C:\xampp\htdocs\subdir\process.php</b> on line <b>8</

例如第 8 行是 $target_file = $target_dir 。rand() .basename($_FILES["fileToUpload"]["name"]);

当我通过网站成功上传文件时,我用提琴手检查了请求标头,但我没有得到任何可以帮助我的信息。

Fiddler 原始请求标头:

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

上传.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>

进程.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. \nError Code:". $_FILES["fileToUpload"]['error'];
}

标签: phpfilefile-uploaduploadrequest

解决方案


推荐阅读