drh47606 2017-04-24 08:43
浏览 19

通过PHP下载对某些用户不起作用

To download the file I'm using the GET request to the download server like domain.com/?file=/uploads/files/03/48/51/2017/04/09/file.rar

On my computer, like most users, the download goes well. But for some users (about 10-20% of total), there are errors. From their words (different users):

  1. In Chrome in download status Network Error: Network Error
  2. 502 bad gateway
  3. Download file as php.
  4. Antivirus block.
  5. 404 error

            function clean($string) {
              // $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
               //$string = preg_replace('/[^A-Za-z0-9\-\.\_]/', '', $string); // Removes special chars.
    
               return preg_replace('/\.\./', '', $string); // Replaces multiple hyphens with single one.
            }
    
            function file_force_download($file) {
              $file = clean($file);
              if (file_exists($file)) {
                if (ob_get_level()) {
                  ob_end_clean();
                }
                if ($mimetype = mime_content_type($file)) {} else $mimetype="application/octet-stream";
                header('Content-Description: File Transfer');
                header('Content-Type:.'.$mimetype);
                header('Content-Disposition: attachment; filename=' . basename($file));
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file));
    
                if ($fd = fopen($file, 'rb')) {
                  while (!feof($fd)) {
                    print fread($fd, 1024);
                  }
                  fclose($fd);
                }
                exit("Error while reading file");
              } exit("File does not exist");
            }
    
    
            $file = $_GET['file'];
            $file = preg_replace('/uploads\/files\//', '', $file);
            if (preg_match("/\d\d\/\d\d\/\d\d\/\d\d\d\d\/\d\d\/\d\d\/.{1,60}/i", $file)) {
    
             file_force_download("uploads/files/".$file);
            } else exit ("Incorrect file link");
    
        ?>
    

I tried changing the download server and the domain does not help. Why this happens?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stable diffusion
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算