我想使用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命令?


当前回答

我用这个命令curl -F 'filename=@/home/ yourhomedirectory /file.txt' http://yourserver/upload

其他回答

你需要使用-F选项: -F/——form <name=content>指定HTTP多部分POST数据

试试这个:

curl \
  -F "userid=1" \
  -F "filecomment=This is an image file" \
  -F "image=@/home/user1/Desktop/test.jpg" \
  localhost/uploader.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

这对我来说很管用。

我的虚拟机崩溃了,它只有互联网连接。 我用这种方法恢复了一些文件。

下面是如何正确转义任意文件名的上传文件与bash:

#!/bin/bash
set -eu

f="$1"
f=${f//\\/\\\\}
f=${f//\"/\\\"}
f=${f//;/\\;}

curl --silent --form "uploaded=@\"$f\"" "$2"
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"
}

作为curl的替代方案,您可以使用HTTPie,它是一个CLI,类似于curl的人类工具。

安装说明:https://github.com/jakubroztocil/httpie#installation 然后,运行: http -f POST http://localhost:4040/api/users username=johnsnow photo@images/avatar.jpg Http /1.1 200 ok Access-Control-Expose-Headers: X-Frontend cache - control:不是商店 连接:维生 内容编码:gzip 内容长度:89 内容类型:text / html;charset = windows - 1251 时间:2018年6月26日星期二11:11:55 GMT 编译指示:no - cache Apache服务器: 不同:接受编码 X-Frontend: front623311 ...