Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CURL PHP send image

Tags:

php

curl

It's trivial to get image from server but I think about something different. It is crazy question but... Is it possible to send file (image) to a server but not using form upload or ftp connection? I want to send a request to eg. http://www.example.com/file.php with binary content. I think I need to set Content-type header image/jpeg but how to add some content to my request?

like image 397
xxxZonexxx Avatar asked Aug 08 '10 08:08

xxxZonexxx


People also ask

What is CURLFile?

CURL upload file allows you to send data to a remote server. The command-line tool supports web forms integral to every web system.


2 Answers

there are multiple ways to use curl to upload image files, e.g.:

$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@/path/to/image.jpeg');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
//CURLOPT_SAFE_UPLOAD defaulted to true in 5.6.0
//So next line is required as of php >= 5.6.0
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);

you can check the examples at: http://au.php.net/manual/en/function.curl-setopt.php

like image 130
Andy Lin Avatar answered Sep 28 '22 03:09

Andy Lin


see http://docs.php.net/function.curl-setopt:

CURLOPT_POSTFIELDS The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.
like image 40
VolkerK Avatar answered Sep 28 '22 03:09

VolkerK