19

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?

xxxZonexxx
  • 191
  • 1
  • 1
  • 3

6 Answers6

21

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

muglio
  • 2,048
  • 17
  • 18
Andy Lin
  • 545
  • 2
  • 4
10

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.
VolkerK
  • 95,432
  • 20
  • 163
  • 226
5

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.

Husam
  • 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
2

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.

Sayaman
  • 1,145
  • 4
  • 14
  • 35
1

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);
Egor
  • 21
  • 2
0

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

Shubham
  • 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