PHP uploads files using CURL | downloads files using CURL to download files
/*** @ Param string $ img_url address of the downloaded file * @ param string $ save_path directory * @ param string $ filename name of the downloaded file * @ return bool */function curlDownFile ($ img_url, $ save_path = '', $ filename ='') {if (trim ($ img_url) = '') {return false;} if (trim ($ save_path) = '') {$ save_path = '. /';} // create and save the directory if (! File_exists ($ save_path )&&! Mkdir ($ save_path, 0777, true) {return false;} if (trim ($ filename) = '') {$ img_ext = strrchr ($ img_url ,'. '); $ img_exts = array('.gif', '.jpg ', '.png'); if (! In_array ($ img_ext, $ img_exts) {return false;} $ filename = time (). $ img_ext;} // curl download file $ ch = curl_init (); $ timeout = 5; curl_setopt ($ ch, CURLOPT_URL, $ img_url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout); $ img = curl_exec ($ ch); curl_close ($ ch ); // Save the file to the specified path file_put_contents ($ filename, $ img); unset ($ img, $ url); return true;} // after the function is executed, the downloaded image curlDownFile (' http://mimg.127.net/logo/163logo.gif ');
Use CURL to upload files 1. before PHP5.5
First writing method, PHP version requirements <5.5
$ Filename is the file path and must have
Filename=test.txt is the name of the file received by the receiver. if it is null, take the basename part in the filename File path.
Type = text/plain document type, which can be blank
/*** @ Param string $ target_url destination address for uploading * @ param string $ filename File path * @ param string $ form_name form name */function curlUploadFile ($ target_url, $ filename, $ form_name) {$ post_data = array ($ form_name => "@?filename=filename=test.txt; type = text/plain",); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ target_url); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); $ result = curl_exec ($ ch); curl_close ($ ch);} $ target_url = 'http: // www.codean.net/notfound/test.php'?#filename = realpath ("C: /Users/HelloWorld/Desktop/Images/1.jpg"); $ form_name = 'file'; // the receiver uses $ _ FILES to accept curlUploadFile ($ target_url, $ filename, $ form_name );
2. use CURL to upload files after PHP5.5
Method 2 (recommended), PHP version> = 5.5
Absolute path is recommended for files to be uploaded by filename.
Mimetype: The default value is application/octet-stream. leave it blank.
Postname: name of the file in the $ _ FILES array of the receiver. the default value is the file name.
/*** @ Param string $ target_url destination address for uploading * @ param string $ filename File path * @ param string $ form_name form name */function curlUploadFile ($ target_url, $ filename, $ form_name) {$ upload_file = new CURLFile ($ filename); $ post_data = array ($ form_name => $ upload_file); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ target_url); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data); curl_setopt ($ ch, expires, 1 ); $ result = curl_exec ($ ch); curl_close ($ ch);} $ target_url = 'http: // www.codean.net/notfound/test.php'?#filename = realpath ("C: /Users/HelloWorld/Desktop/Images/1.jpg"); $ form_name = 'file'; // the receiver uses $ _ FILES to accept curlUploadFile ($ target_url, $ filename, $ form_name );
3. PHP sends a file stream to upload a file
/** Method 3: Use the PHP stream to send * @ param string $ target_url to upload the target address */function curlUploadFile ($ target_url) {$ fh = fopen ('php: // temp ', 'rw +'); $ string = 'Hello World'; fwrite ($ fh, $ string); rewind ($ fh ); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ target_url); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt ($ ch, CURLOPT_PUT, true ); curl_setopt ($ ch, CURLOPT_INFILE, $ fh); curl_setopt ($ ch, CURLOPT_INFILESIZE, strlen ($ string); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); $ result = curl_exec ($ ch); curl_close ($ ch);} $ target_url = 'http: // www.codean.net/notfound/test.php'{curluploadfile ($ target_url ); // the receiving end extracts the stream file and saves $ putdata = fopen ('php: // input', 'r'); $ fp = fopen('test.txt ', 'w '); while ($ data = fread ($ putdata, 1024) {fwrite ($ fp, $ data) ;}fclose ($ fp); fclose ($ putdata );
4. Upload files through HTTP packets
/*** @ Param string $ target_url file Upload address */function curlUploadFile ($ target_url) {// Generate the separator $ delimiter = '-------------'. uniqid (); // array of files to be uploaded $ fileFields = array ('file' => array ('name' => 'test.png ', 'type' => 'text/html', 'content' => 'This is my file content. '),); // array value of $ _ POST accepted by the backend $ postFields = array ('file' => 'File',); $ data = ''; // Generate the body string foreach ($ postFields as $ name => $ content) {$ data. = "--". $ delimiter. "\ r \ n"; $ data. = 'content-Disposition: form-data; name = "'. $ name. '"'; $ data. = "\ r \ n ". $ content. "\ r \ n";} // Generate the uploaded file into the subject string foreach ($ fileFields as $ name => $ file) {$ data. = "--". $ delimiter. "\ r \ n"; $ data. = 'content-Disposition: form-data; name = "'. $ name. '"; filename = "'. $ file ['name']. "\" \ r \ n "; $ data. = 'content-Type :'. $ file ['type']. "\ r \ n"; $ data. = $ file ['content']. "\ r \ n";} // separator for the end of the subject $ data. = "--". $ delimiter. "--"; // curl: uploads a file $ handle = curl_init ($ target_url); curl_setopt ($ handle, CURLOPT_POST, true); curl_setopt ($ handle, CURLOPT_HTTPHEADER, array ('content-Type: multipart/form-data; boundary = '. $ delimiter, 'Content-Length :'. strlen ($ data); curl_setopt ($ handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ handle, CURLOPT_POSTFIELDS, $ data); $ result = curl_exec ($ handle ); curl_close ($ handle );}