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