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?
6 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
-
Thank you such a complete and concise example! – Kenny Wyland Jul 08 '13 at 02:37
-
3CURLOPT_SAFE_UPLOAD defaulted to true in 5.6.0... so you will need to add curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); before setting CURLOPT_POSTFIELDS. – muglio Feb 15 '16 at 16:48
-
I got the error "curl_setopt_array(): Disabling safe uploads is no longer supported". PHP >= 7.3.1 – Pooja Jadav May 20 '19 at 05:42
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¶2=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.

- 95,432
- 20
- 163
- 226
The only code worked for me with PHP 7.0
$file = new \CURLFile('@/path/to/image.jpeg'); //<-- Path could be relative
$data = array('name' => 'Foo', 'file' => $file);
$ch = curl_init();
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);
Thanks to @AndyLin for his answer and this source.

- 8,149
- 3
- 38
- 45
-
https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg is there a way to use URL like this? OR any idea how to do this? i tried but got this. The Response content must be a string or object implementing __toString(), "boolean" given. – M Amir Shahzad Jul 30 '20 at 17:52
The method used by Andy Lin didn't work for me for some reason so I found this method:
function makeCurlFile($file){
$mime = mime_content_type($file);
$info = pathinfo($file);
$name = $info['basename'];
$output = new CURLFile($file, $mime, $name);
return $output;
}
You can send other stuffs, not just files by associating the value to a key inside the $data payload as shown below:
$ch = curl_init("https://api.example.com");
$mp3 =makeCurlFile($audio);
$photo = makeCurlFile($picture);
$data = array('mp3' => $mp3, 'picture' => $photo, 'name' => 'My latest single',
'description' => 'Check out my newest song');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result = curl_error($ch);
}
curl_close ($ch);
I am thinking this is due to the fact that some API doesn't support the old way of doing it for security reasons.

- 1,145
- 4
- 14
- 35
I used this method of sending a photo from the HTML form
$ch = curl_init();
$cfile = new CURLFile($_FILES['resume']['tmp_name'], $_FILES['resume']['type'], $_FILES['resume']['name']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $cfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

- 21
- 2
VolkerK is completely right but my experience suggest that sending a file "@" operator will only work with arrays.
$post['file'] = "@FILE_Path"
Now you can send the file using CURLOPT_POSTFIELDS

- 21,300
- 18
- 66
- 89
-
This method is no longer recommended. See : http://php.net/manual/en/class.curlfile.php – Erik Dec 10 '14 at 03:58