gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 11:29:17 30 4
gpt4 key购买 nike

我一直在尝试在 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com