xxxZonexxx
xxxZonexxx

Reputation: 201

CURL PHP send image

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?

Upvotes: 20

Views: 52697

Answers (6)

Egor
Egor

Reputation: 21

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);

Upvotes: 1

user11092881
user11092881

Reputation:

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.

Upvotes: 2

Husam
Husam

Reputation: 8568

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.

Upvotes: 5

Andy Lin
Andy Lin

Reputation: 545

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

Upvotes: 21

Shubham
Shubham

Reputation: 22307

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

Upvotes: 0

VolkerK
VolkerK

Reputation: 96159

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.

Upvotes: 10

Related Questions