0

This is the past question I've found while I was google. The first answer is pretty simple and I don't understand.

The whole script is here,

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/your-destination-script.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setpopt($ch, CURLOPT_POSTFIELDS, array(
        'file' => '@/..../file.jpg',    // you'll have to change the name, here, I suppose
        // some other fields ?
));
$result = curl_exec($ch);
curl_close($ch);

This is what I don't understand,

curl_setopt($ch, CURLOPT_URL, "http://www.example.com/your-destination-script.php");

this is the place to define destination url. But what should I write in there to upload a file from here,

curl_setpopt($ch, CURLOPT_POSTFIELDS, array(
        'file' => '@/..../file.jpg',    // you'll have to change the name, here, I suppose
        // some other fields ?
));

Please someone explain me or please try. I'm new to PHP.

Community
  • 1
  • 1
JaiSat
  • 2,356
  • 1
  • 14
  • 15

1 Answers1

1

The answer is pretty simple. The destination script(lets say, a PHP file here) is the script that uses the $_POST array to get the file contents using file_get_contents() function(or if a JSON object, it decodes it using json_decode() function)

So, basically, this is the script to which the image is sent for uploading. Can be any script on any site (if the site allows, uploading like this). There are times when sites require a login ID, or a token ID for this, which is also passed by using CURL_POSTFIELDS and checked in the PHP script if the login ID or token ID was real or not. Just a security measure :)

Understood?? :)

Generally, the destination script is a script used to connect to a site's API etc etc :)

prateekkathal
  • 3,482
  • 1
  • 20
  • 40
  • Thank you so much!!! I understood the concept. So the destination script is taking care of the received file. Do you have any sample script? I want simply upload a file from localhost to the remote server. If you have any sample script to handle received files? please!? – JaiSat Mar 05 '14 at 05:57
  • TinyPNG is one site which allows Image upload via CURL, have a look at there [API Reference](https://tinypng.com/developers/reference) here. Scroll down till you see PHP with CURL :) – prateekkathal Mar 05 '14 at 06:11