1. This method can download pictures, compressed packages, and pdf (personal test). All types of files should be downloaded locally. You can try it.
//Remote path, name, file suffixfunction downImgRar($url,$rename,$ext){ switch ($ext) { case 'jpg': //Download pictures $file_path = 'uploads/images/'; break; case 'png': //Download pictures $file_path = 'uploads/images/'; break; case 'pdf': //Download PDF $file_path = 'uploads/pdf/'; break; case 'rar': //Download the compressed package $file_path = 'uploads/rar/'; break; case 'zip': //Download the compressed package $file_path = 'uploads/rar/'; break; default: $file_path = 'uploads/files/'; break; } $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); $rawdata=curl_exec ($ch); curl_close ($ch); // Use Chinese file name to transcode $fp = fopen($file_path.iconv('UTF-8', 'GBK', $rename).".".$ext,'w'); fwrite($fp, $rawdata); fclose($fp); // Return path return $_SERVER['DOCUMENT_ROOT'].$file_path.$rename.".".$ext; }
2. Download the compressed file, this can only download the compressed file
// Download the compressed packagefunction downRar($file_path) { $file_name = '/uploads/rar/2009323162920-Vision C Yinqiao Film Manual.rar'; $file_name = iconv("utf-8","gbk//IGNORE",$file_name); // Pay special attention! Pay special attention! Pay special attention here. Transcoding must be turned on under Windows, otherwise the direct file will not be stored $file_path = $_SERVER['DOCUMENT_ROOT'] . $file_name;// For example, here under Windows, mine is "D:/web/public/uploads/rar/2009323162920-WeChat Yinqiao Film Manual.rar" //Judge if the file exists, jump to the download path if (!file_exists($file_path)) { die("The file does not exist!"); } $fp = fopen($file_path, "r+") or die('Open file error'); //The file must be opened first when downloading it. Write to memory $file_size = filesize($file_path); //Returned file stream Header("Content-type:application/octet-stream"); //Return in byte format Header("Accept-Ranges:bytes"); //Return file size Header("Accept-Length:" . $file_size); //The client dialog box pops up, the corresponding file name Header("Content-Disposition:attachment;filename=" . substr($file_name, strrpos($file_name, '/') + 1)); //Prevent the server from increasing pressure instantly and read in segments $buffer = 1024; while (!feof($fp)) { $file_data = fread($fp, $buffer); echo $file_data; } fclose($fp); die("Download successfully!"); }
Summarize
The above is the php introduced by the editor to you by downloading remote pictures, compressed packages, PDFs and other files to the local area according to the URL. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!