我想使用cURL不仅在HTTP POST中发送数据参数,而且还上传具有特定表单名称的文件。我该怎么做呢?
HTTP Post参数:
Userid = 12345
filecomment =这是一个映像文件
文件上传:
文件位置= /home/user1/Desktop/test.jpg
file = image的表单名称(对应PHP端$_FILES['image'])
我认为cURL命令的一部分如下所示:
curl -d "userid=1&filecomment=This is an image file" --data-binary @"/home/user1/Desktop/test.jpg" localhost/uploader.php
我遇到的问题如下:
Notice: Undefined index: image in /var/www/uploader.php
问题是我使用$_FILES['image']在PHP脚本中拾取文件。
如何相应地调整cURL命令?
保存所有已发送文件到文件夹:
主机上的PHP文件。u.php:
<?php
$uploaddir = 'C:/VALID_DIR/';
echo '<pre>';
foreach ($_FILES as $key => $file) {
if(!isset($file) || !isset($file['name'])) continue;
$uploadfile = $uploaddir . basename($file['name']);
if (move_uploaded_file($file['tmp_name'], $uploadfile)) {
echo "$key file > $uploadfile .\n";
} else {
echo " Error $key file.\n";
}
}
print_r($_FILES);
print "</pre>";?>
客户端使用:
curl -v -F filename=ff.xml -F upload=@ff.xml https://myhost.com/u.php
这对我来说很管用。
我的虚拟机崩溃了,它只有互联网连接。
我用这种方法恢复了一些文件。
保存所有已发送文件到文件夹:
主机上的PHP文件。u.php:
<?php
$uploaddir = 'C:/VALID_DIR/';
echo '<pre>';
foreach ($_FILES as $key => $file) {
if(!isset($file) || !isset($file['name'])) continue;
$uploadfile = $uploaddir . basename($file['name']);
if (move_uploaded_file($file['tmp_name'], $uploadfile)) {
echo "$key file > $uploadfile .\n";
} else {
echo " Error $key file.\n";
}
}
print_r($_FILES);
print "</pre>";?>
客户端使用:
curl -v -F filename=ff.xml -F upload=@ff.xml https://myhost.com/u.php
这对我来说很管用。
我的虚拟机崩溃了,它只有互联网连接。
我用这种方法恢复了一些文件。
cat test.txt
文件test.txt内容。
curl -v -F "hello=word" -F "file=@test.txt" https://httpbin.org/post
> POST /post HTTP/2
> Host: httpbin.org
> user-agent: curl/7.68.0
> accept: */*
> content-length: 307
> content-type: multipart/form-data; boundary=------------------------78a9f655d8c87a53
>
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
* We are completely uploaded and fine
< HTTP/2 200
< date: Mon, 15 Nov 2021 06:18:47 GMT
< content-type: application/json
< content-length: 510
< server: gunicorn/19.9.0
< access-control-allow-origin: *
< access-control-allow-credentials: true
<
{
"args": {},
"data": "",
"files": {
"file": "file test.txt content.\n"
},
"form": {
"hello": "word"
},
"headers": {
"Accept": "*/*",
"Content-Length": "307",
"Content-Type": "multipart/form-data; boundary=------------------------78a9f655d8c87a53",
"Host": "httpbin.org",
"User-Agent": "curl/7.68.0",
"X-Amzn-Trace-Id": "Root=1-6191fbc7-6c68fead194d943d07148860"
},
"json": null,
"origin": "43.129.xx.xxx",
"url": "https://httpbin.org/post"
}