Compatible with the PHP5 and php7 Curl File upload function

Source: Internet
Author: User
This article mainly introduces the compatibility of PHP5 and PHP7 of the Curl file upload function, combined with the case analysis of PHP5 and PHP7 version in the use of Curl file upload relevant decision and specific operation skills, the need for friends can refer to the next

This article describes the Curl file upload feature that is compatible with PHP5 and PHP7. Share to everyone for your reference, as follows:

Why write This example

Recently modified a project, you need to upload files via curl.

Remember to do a similar implementation before, so the code before the turn, using the "@" prefix method.

But the same method is not now! It was later discovered that version compatibility was a problem.

Running the spirit of open source sharing, while avoiding their own forgotten, so wrote the following example program.

Sample Program

Special Note:

A total of 3 files are placed in the test directory of the Web root directory, ensuring that the directory is writable. The uploaded image will also be saved in that directory.

If you want to put the program files in a different directory, you must change the relevant URLs in your PHP code, otherwise the example might not work.


<?php/** * Receive files uploaded via browser * * @author straiway<straiway@qq.com> * @site http://straiway.sinaapp.com */if (Empty ($_ files[' file ']) {exit (' No files with the specified name uploaded ');} Save to local, then upload $file = $_files[' file ']; $file _name = __dir__. "/{$file [' Name ']}"; Move_uploaded_file ($_files[' file '] [' tmp_name '], $file _name);//local test, you may need to change the following url$ch = Curl_ Init (' http://localhost/test/upload_via_curl.php ');//starting from php5.5, against using the "@" prefix upload, you can use the curlfile substitution;//It is said that php5.6 began to remove the "@"  Prefix upload method if (class_exists (' Curlfile ')) {$file = new Curlfile ($file _name); Disable the "@" upload method so that you can safely transfer "@" at the beginning of the parameter value curl_setopt ($ch, Curlopt_safe_upload, true);} else {$file = "@{$file _name}";} Starting with php5.2, to upload a file, you must pass an array to Curlopt_postfields instead of a string. Only the array is passed, and the HTTP header "Content-type" is set to "Multipart/form-data" curl_setopt ($ch, Curlopt_postfields, Array (' File_via_ Curl ' = $file));//Transfer result as curl_exec return value instead of direct output curl_setopt ($ch, Curlopt_returntransfer, true); $result = curl_exec ($ch); $error = Curl_error ($ch), if ($result) {$result _array = Json_decode ($result, trUE); if ($result _array) {if ($result _array[' status ']) {exit ("Upload successful!curl return image address: {$result _array[' data '] [' url ']}<br/&G    T; ');    } else {exit ("Curl upload failed! Error message: {$result [' info ']}");  }} else {exit ("error occurred, Curl returns result: {$result}"); }} else {exit (' Curl request error '. Var_export ($error, True));}

<?php/** * Accept files uploaded via Curl. * * @author straiway<straiway@qq.com> * @site  http://straiway.sinaapp.com */if (Empty ($_files[' File_via_ Curl '])) {  $return = array (' status ' = = 0, ' info ' = ' No file with the specified name ');} else {  //save file  $file = $_files[' fil E_via_curl '];  Rename files for easy identification  $base _name = Explode ('. ', $file [' name ']);  $base _name[0]. = ' _upload_var_curl ';  $base _name = Implode ('. ', $base _name);  $file _name = __dir__. "/{$base _name}";  if (Move_uploaded_file ($file [' Tmp_name '], $file _name)) {    //local test, you may need to change the following URL    $url  = "http// localhost/test/{$base _name} ";    $return = Array (' status ' = = 1, ' info ' = ' upload succeeded ', ' data ' = ' array ' (' url ' = ' = ' $url ');  } else {    $return = array (' status ' = = 0, ' info ' = ' upload failed ');}  } Exit (Json_encode ($return));

Related Article

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.