作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我一直在尝试在 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/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!