php - 通过发布请求传输文件
问题描述
我对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'];
}
解决方案
推荐阅读
- dynamic - 使用 istio 根据流量的错误响应将流量分流到两个版本
- c++ - 如何打印出 DECIMAL (tagDEC) 类型的值?
- r - 如何在R中双重集成一个函数
- r - 在 dplyr mutate/summarise 中应用函数和传播数据
- android - Android AppUpdateManager 未完成立即更新
- linux - 安装塔 RPM 任务失败
- android - Android 7 之前的 SwitchPreference 总是关闭
- android - 列出 Android Q / API 29 上的 WiFi 网络
- scala - 如何在微服务架构中从一台服务器下载多个文件并同时上传到另一台服务器(并发管道)
- python - 为什么 ax.twinx() 会导致这种行为?