linux - 在 linux 命令行中使用 curl --data 上传 cURL http post 文件

标签 linux http post curl

我一直在尝试在 linux 命令行中使用此命令将文件上传到服务器:

curl -v --data 'file=@/var/www/fbapp/images/registration.png' http://myserver/xmltest/curlupload.php

但它没有将文件上传到服务器,即使它正在发回响应。

虽然如果我使用此命令,它会上传文件并发回响应:

curl -v -F 'filename=registration.png' -F 'file=@/var/www/fbapp/images/registration.png' http://myserver/xmltest/curlupload.php

但我希望命令本身采用 curl --data 格式。

这是我在服务器中的 PHP 代码:

<?php
$uploadpath = "images/";
$filedata = $_FILES['file']['tmp_name'];
echo "filedata= ".$filedata;
if ($filedata != '')
copy($filedata,$uploadpath."1.png");
echo "success";
?>

这是我返回的 header 响应:

> POST /xmltest/curlupload.php HTTP/1.1
> User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: myserver
> Accept: */*
> Content-Length: 44
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 44out of 44 bytes
< HTTP/1.1 200 OK
< Date: Thu, 06 Nov 2014 07:01:56 GMT
< Server: Apache
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
< Content-Type: text/html
< 
* Connection #0 to host myserver left intact
* Closing connection #0
filedata= success

如何使用curl --data 上传文件?

最佳答案

您通常不能随意选择 -F 或 -d (--data)。将接收您的帖子的网络服务器需要其中一种格式。如果您尝试提交的表单使用“multipart/form-data”类型,那么您必须使用 -F 类型。如果不是,您应该使用 -d,这会导致发布类型为“application/x-www-form-urlencoded”。

带有 -F 的 multipart/formposts 使用特殊格式的帖子,Mime-headers 将不同的部分分开,每个部分都有自己的一组标题。

-d 只是发送给服务器解释/解码的原始数据。

curl FAQ 中提到.

(但是由于您在这种情况下编写了 PHP 代码,所以您只需决定要接受哪种 POST 方法,然后让您的 curl 命令行使用该方法。)

关于linux - 在 linux 命令行中使用 curl --data 上传 cURL http post 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26773724/

相关文章:

ruby-on-rails - 等/bash.bashrc : line 2: GREP_OPTIONS: command not found

linux - 链接到 linux 中的特定共享库版本

.net - 如果外部命令的输出是无意义的,最好抛出异常?

AF_UNIX 上的 HTTP : HTTP connection to unix socket

php - 从数据库中删除打印结果的最佳方法

ruby-on-rails - 通过 HTTPS 发布 Ruby on Rails

c - "fork()"后printf异常

android - Android 上的 Youtube - 如何检查它使用的协议(protocol)?

java - 使用 Amazon Spot 实例(或其他方式)从 Java 代码发出多个 HTTP 请求

http - 为什么我的上传进度条在传输完成之前就结束了?