How PHP uses CURL to upload images to a remote server-PHP source code

Source: Internet
Author: User
We use ftp to upload files in php at most, but curl can also be implemented so that you can directly upload files without ftp, let's take a look at how PHP uses CURL to upload images to a remote server. We use ftp to upload files in php at most, but curl can also be implemented so that you can directly upload files without ftp, let's take a look at how PHP uses CURL to upload images to a remote server.

Script ec (2); script


If the image and other resource servers are remote, you can upload images to the remote server by using cURL. This article briefly introduces how PHP uses cURL to upload images to the remote server.


Send the image code snippet through cURL:


$ Data = array ('img '=>' @ '. dirname (_ FILE _).'/img/1.jpg ');

Note: After PHP5.5, @ cannot be used to indicate files.

Adding the @ syntax before the file name indicates that the file is uploaded. This is normal in PHP5.3, but the @ syntax is completely abolished in PHP5.6, resulting in the inability to upload images.


// Supports remote file upload
If (empty ($ urlinfo ['host']) {
$ Tmp_name = dirname ($ file ['tmp _ name']). '/'. $ file ['title']. '. '. $ file ['extension']; // Add the file suffix
Rename ($ file ['tmp _ name'], $ tmp_name );
$ Fields ['file'] = '@'. $ tmp_name; // Add the @ symbol curl to treat it as a file upload process.
} Else {
$ Fields ['url'] = $ file ['tmp _ name'];
}

Line 3: Add @ to the front of the file name. curl treats it as a file upload. This is normal in PHP5.3. The cURL file upload code is 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 indicates uploading files:


Curl_setopt (ch, CURLOPT_POSTFIELDS ,[
'File' => new CURLFile(realpath('image.png ')),
]);

The CURLFile method is newly added in PHP5.5. The @ syntax is completely abolished in PHP5.6. Therefore, either the PHP5.3 method or the PHP5.6 method is used. The two methods can only be selected and cannot be compatible. There is no way, you can use:


If (version_compare (phpversion (), '5. 4.0 ')> = 0)

This function is used to determine the PHP version selection method. However, this method is not recommended and it is best to unify the environment.

If you must be compatible with different versions of PHP, follow these steps:


If (empty ($ urlinfo ['host']) {
$ Tmp_name = dirname ($ file ['tmp _ name']). '/'. $ file ['title']. '. '. $ file ['extension']; // Add the 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; // Add the @ symbol curl to treat 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 );

Then, the code snippet of up. php on the Remote Server accepts the request to save the image:

If ($ _ FILES ){
$ Filename = $ _ FILES ['img '] ['name'];
$ Tmpname = $ _ FILES ['img '] ['tmp _ name'];
If (move_uploaded_file ($ tmpname, dirname (_ FILE _). '/upload/'. $ filename )){
Echo json_encode ('upload successful ');
} Else {
$ Data = json_encode ($ _ FILES );
Echo $ data;
}
}
It is actually the same as local file transfer. It only transfers the file through cURL, or uses $ _ FILES to receive and save the file.

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.