The principle of PHP using curl to upload images to a remote server

Source: Internet
Author: User
Tags curl curl upload file file upload


If the image and other resource servers are remote, image upload can be curl way to the remote server, this article briefly describes the use of PHP curl to the remote server upload the image principle.


To transfer a picture code fragment via curl:


$data = Array (' img ' => ' @ '. DirName (__file__). " /img/1.jpg ');

Note that you cannot use @ to represent files after PHP5.5.

It is normal in PHP5.3 to add the @ syntax to the file name before the filename, but the @ syntax is completely abolished in PHP5.6, resulting in the upload image being unusable.


Support for remote file uploads
if (Empty ($urlinfo [' Host '])) {
$tmp _name=dirname ($file [' tmp_name ']). ' /'. $file [' title ']. '. $file [' extension '];//plus file suffix
Rename ($file [' Tmp_name '], $tmp _name);
$fields [' file '] = ' @ '. $tmp _name;//@ symbol Curl will take it as a file upload process
}else{
$fields [' url ']= $file [' Tmp_name '];
}

Line 5th, with the @ match before the filename, curl will treat it as a file upload, which is normal in PHP5.3. Curl upload file code as follows:


$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $this->config[' PostURL '));
curl_setopt ($ch, curlopt_post,true);
curl_setopt ($ch, curlopt_returntransfer,true);
curl_setopt ($ch, curlopt_connecttimeout, 1); Connection Timeout
curl_setopt ($ch, Curlopt_postfields, $fields);
$data =curl_exec ($ch);
$info =curl_getinfo ($ch);
Curl_close ($ch);

In PHP5.6, the Curlopt_postfields parameter in Curl writes the upload file:


curl_setopt (CH, curlopt_postfields, [
' File ' => new Curlfile (Realpath (' image.png ')),
]);

The Curlfile method is a new addition in the PHP5.5, which completely abolishes the @ syntax in PHP5.6, so either the PHP5.3 method or the PHP5.6 method can only be selected and incompatible. There is no way, you can use:


if (Version_compare (Phpversion (), ' 5.4.0 ') >= 0)

This function to determine the PHP version to choose a different way, but this way is not recommended, or the best unified environment.

If you must be compatible with different versions of PHP, refer to the following:


if (Empty ($urlinfo [' Host '])) {
$tmp _name=dirname ($file [' tmp_name ']). ' /'. $file [' title ']. '. $file [' extension '];//plus file suffix
Rename ($file [' Tmp_name '], $tmp _name);
if (Version_compare (Phpversion (), ' 5.5.0 ') >= 0 && class_exists (' Curlfile ')) {
$fields [' file '] = new Curlfile (Realpath ($tmp _name));
}else{
$fields [' file '] = ' @ '. $tmp _name;//@ symbol Curl will take it as a file upload process
}
}else{
$fields [' url ']= $file [' Tmp_name '];
}
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $this->config[' PostURL '));
curl_setopt ($ch, curlopt_post,true);
curl_setopt ($ch, curlopt_returntransfer,true);
curl_setopt ($ch, curlopt_connecttimeout, 1); Connection Timeout
curl_setopt ($ch, Curlopt_postfields, $fields);
$data =curl_exec ($ch);
$info =curl_getinfo ($ch);
Curl_close ($ch);

The remote server then up.php an example of a code fragment that accepts a request to save a picture:

if ($_files) {
  $filename = $_files[' img ' [' name '];
  $tmpname = $_files[' img ' [' tmp_name '];
& nbsp if (Move_uploaded_file ($tmpname, DirName (__file__). /upload/'. $filename)) {
     echo json_encode (' upload succeeded ');
 }else{
      $data = Json_encode ($_files);
     Echo $data;
 }
}
It is actually the same as the local file-passing principle, simply passing the file through curl or using $_files to receive the file for saving.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.