0

I've been on this for 3 days now but I can't seem to resolve it, the user will input the url and the system will upload it on remote server.. this is what I've done so far

$POST_DATA = array(
    'file' => '@'.  'http://images.visitcanberra.com.au/images/canberra_hero_image.jpg',
    'extension' => 'txt',
    'filename' => 'test',
    'directory' => 'uploads/'
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://1234.4321.67.11/upload.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
$response = curl_exec($curl);

curl_close ($curl);
WeSt
  • 2,628
  • 5
  • 22
  • 37
eaponz
  • 574
  • 1
  • 16
  • 32
  • I think you need to set the type to multipart/formdata else you'll get uriencoded which can't carry files. – Jasen Jan 12 '15 at 08:50
  • Are you getting an error? I suspect the upload filename has to be a local file, not a URL. So you might have to download the file to a local file first, then upload it. – Barmar Jan 12 '15 at 08:52

3 Answers3

0

Well do it in this way.

  1. First download that file from url provided by user.
  2. Upload that image on http://1234.4321.67.11/upload.php
  3. And delete that file after successfully uploaded on server.

This might help you for uploading image on server using cURL : https://stackoverflow.com/a/15177543/1817160

And this might help you in understanding how to download image from url : Copy Image from Remote Server Over HTTP

Community
  • 1
  • 1
Anjum
  • 681
  • 3
  • 14
  • 38
0

Use the following function to upload the remote image using curl. Call this by passing image url and path to folder to save the image.

function grab_image($url,$saveto){
        $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        $raw=curl_exec($ch);
        curl_close ($ch);
        if(file_exists($saveto)){
            unlink($saveto);
        }
        $fp = fopen($saveto,'x');
        fwrite($fp, $raw);
        fclose($fp);
    }

and ensure that in php.ini allow_url_fopen is enable

Needhi Agrawal
  • 1,326
  • 8
  • 14
0
$file_name_with_full_path = realpath('./upload/example.png');
$curl_handle = curl_init("https://example.com");
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = new CurlFile($file_name_with_full_path, 'image/png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args); 
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$returned_data = curl_exec($curl_handle);
curl_close ($curl_handle);
echo $returned_data;
Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47
Harry baldaniya
  • 167
  • 1
  • 11
  • Why should the OP "try this"? A **good answer** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer. – Maximilian Ast Oct 18 '16 at 07:12