php downloads remote pictures compressed packages pdf and other files to the local according to URL

  • 2021-12-13 16:25:26
  • OfStack

1. This method can download pictures, compressed packages, pdf (pro-test), all types of files should be downloaded to the local, you can try 1


// Remote path, name, file suffix 
function 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);
  //  Transcoding is required to use Chinese file names 
  $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 compressed files. This can only download compressed files


//  Download the compressed package 
function downRar($file_path)
{
  $file_name = '/uploads/rar/2009323162920- Dimension C Instructions for Yinqiao tablets .rar';
  $file_name = iconv("utf-8","gbk//IGNORE",$file_name); //  Pay special attention! Pay special attention! Pay special attention here, windows Transcoding must be turned on under , Otherwise, direct files will not be saved 
  $file_path = $_SERVER['DOCUMENT_ROOT'] . $file_name;//  For example windows Down here mine is  "D:/web/public/uploads/rar/2009323162920- Dimension C Instructions for Yinqiao tablets .rar"
  // Determine if the file exists , Then jump to the download path 
  if (!file_exists($file_path)) {
    die(" File does not exist !");
  }
  $fp = fopen($file_path, "r+") or die(' Error opening file ');  // To download a file, you must open the file first. Write to memory 
  $file_size = filesize($file_path);
  // File stream returned 
  Header("Content-type:application/octet-stream");
  // Returns in byte format 
  Header("Accept-Ranges:bytes");
  // Returns file size 
  Header("Accept-Length:" . $file_size);
  // The client dialog box pops up, and the corresponding file name 
  Header("Content-Disposition:attachment;filename=" . substr($file_name, strrpos($file_name, '/') + 1));
  // Prevent the instantaneous pressure of the server from increasing and reading in sections 
  $buffer = 1024;
  while (!feof($fp)) {
    $file_data = fread($fp, $buffer);
    echo $file_data;
  }
  fclose($fp);
  die(" Download succeeded !");
}

Summarize


Related articles: