cURL http post文件上传在linux命令行中使用curl --data [英] cURL http post file upload using curl --data in linux command line

查看:805
本文介绍了cURL http post文件上传在linux命令行中使用curl --data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to upload the file to the server using this command in linux command line:

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

但是它不会将文件上传到服务器,即使它正在发送响应。

but it is not uploading the file to the server, even though it is sending the response back.

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

Although if I use this command it will upload the file and send the response back:

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

但我想要的 curl --data 格式本身的命令。

But I wanted the command in the curl --data format itself.

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

This is my PHP code in the server:

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

这是我回来的标题回复:

And this is the header response that I get back:

> 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)。将接收您的帖子的Web服务器需要其中一种格式。如果你尝试提交的表单使用类型multipart / form-data,那么你必须使用-F类型。如果没有,你应该使用-d然后使用类型'application / x-www-form-urlencoded'的发布。

You usually can't simply pick -F or -d (--data) at your choice. The web server that will receive your post expects one of the formats. If the form you're trying to submit uses the type 'multipart/form-data', then and only then you must use the -F type. If not, you should use -d which then causes a posting with the type 'application/x-www-form-urlencoded'.

multipart / formposts with -F uses一个特殊格式化的帖子,其中Mime-headers分隔不同的部分,每个部分都有自己的一组头。

multipart/formposts with -F uses a special formatting of the post with Mime-headers separating the different parts and each part having its own set of headers.

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

-d is just raw data being sent for the server to interpret/decode.

curl常见问题

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

(But since you write the PHP code in this case, you just have to decide which POST method you want to accept and then make your curl command line use that.)

这篇关于cURL http post文件上传在linux命令行中使用curl --data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆