Php + curl Upload file to remote server-php Tutorial

Source: Internet
Author: User
Tags curl upload file
Php + curl Upload file to remote server 1. install php_curl Support in Windows

1. copy the three php_curl.dll files (possibly in the ext folder), libeay32.dll and ssleay32.dll files in the PHP installation folder to % windir %/system32;
2. open php. ini (which may be in the installation directory of the PHP environment, which is in the c: \ WINDOWS directory by default) and remove the semicolon in the; extension = php_curl.dll header;

3. restart the php service (apache or IIS), such as restarting IIS and running iisreset.

II. considerations.

Note the permissions of the server folder,

In php. ini, upload_tmp_dir sets a brand new directory and also grants Everyone read and write permissions.

The target Directory of the uploaded file also grants the read and write permissions to Everyone.

3. directly add the code

1. file curlTest. php:

 0, 'message' => 'OK'); ini_set ('upload _ max_filesize ', '20m'); ini_set ('post _ max_size', '20m '); ini_set ('memory _ limit ', '128m'); // remote address for receiving uploads $ url = isset ($ _ POST ['URL'])? Trim ($ _ POST ['URL']): ''; $ url =" www.123.com/k1/upload.php "; if (empty ($ url )) {$ result ['code'] = 40001; $ result ['message'] = 'URL cannot be blank '; // Helper_Http: writeJson (200, $ result );} $ file = $ _ FILES ['file']; print_r ($ _ FILES); if ($ file ['error']! = 0) {$ result ['code'] = 40001; $ result ['message'] = 'upload error '; // Helper_Http: writeJson (200, $ result ); echo 1;} else {$ filename = $ file ['name']; $ tmpfile = $ file ['tmp _ name']; $ filetype = $ file ['type']; $ data = upload_file ($ url, $ filename, $ tmpfile, $ filetype); echo $ data; print_r ($ url ); die;}/*** curl Upload file ** @ param unknown $ url * @ param unknown $ filename * @ param unknown $ path * @ param unknow N $ type */function upload_file ($ url, $ filename, $ path, $ type) {echo 3; echo $ path; // php 5.5 or above usage if (class_exists ('\ CURLFile') {$ data = array ('file' => new \ CURLFile (realpath ($ path ), $ type, $ filename);} else {$ data = array ('file' => '@'. realpath ($ path ). "; type = ". $ type. "; filename = ". $ filename);} echo 'data: '; print_R ($ data); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_POST, true); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); curl_setopt ($ ch, CURLOPT_HEADER, false); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); $ return_data = curl_exec ($ ch); curl_close ($ ch); echo $ return_data ;}?>

2. filesUpload. Php:

 "; Print_r ($ _ FILES); $ uploaddir = 'd:/temp/caches/'; $ uploadfile = $ uploaddir. basename ($ _ FILES ['file'] ['name']); echo'
'; if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Possible file upload attack!\n"; }  echo 'Here is some more debugging info:'; ?>


Reference



Http://bbs.csdn.net/topics/300018585

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.