Reputation: 341
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
But I wanted the command in the curl --data format itself.
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
How can I use curl --data to upload the file?
Upvotes: 3
Views: 36092
Reputation: 58004
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 a special formatting of the post with Mime-headers separating the different parts and each part having its own set of headers.
-d is just raw data being sent for the server to interpret/decode.
Mentioned in the curl FAQ.
(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.)
Upvotes: 8