有php下载的问题

问题描述 投票:-2回答:1

所以我以前遇到过这个问题,但这是一个PHP版本的问题。现在情况并非如此。

从来没有改变过代码,而且工作正常。仅改变主机,但我不认为应该有一个问题。

我得到的唯一的东西是垃圾代码。

链接下载是这样的:https://example.com/forum/download.php?file=TEST.zip

<?php

$file = 'TEST.zip';
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header("Content-Type: application/octet-stream");
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    while (ob_get_level()) {
        ob_end_clean();
    }
    readfile($file);
    exit;
}

?>
php force-download
1个回答
0
投票

最好的我可以用有限的代码构成这是删除这个东西?>我讨厌这个标签,我从来没有亲自使用它。

如果PHP之后没有任何内容,就不需要它,如果像这样,它实际上可能会破坏你的文件下载

?>
//empty line here

由于PHP的工作方式,它将输出TAG之外的任何内容作为内容,因此在它们之前或之后的任何内容都可能会在您的文件中结束,具体取决于调用的位置等。

我想exit会照顾它,但我不喜欢那个标签。因为如果你在你的应用程序中包含一堆PHP文件,它只需要一个带有标签之外空格的文件就可以搞砸了,祝你好运。

这一点

while (ob_get_level()) {
    ob_end_clean();
}

如果输出缓冲区已设置,则清除输出缓冲区,如果这是您的文件的全部内容,则可能未设置。就个人而言,我会这样做:

<?php  
 //start output buffering
 ob_start();

//be careful with casing
// - Windows is case insensitive
// - Linux is case sensitive
//for example if the file is named text.zip
//it will work on Windows, but be mission on Linux

$file = 'TEST.zip';

//without the file no download can happen
// -- so kill it before header are sent
// -- that way if file is missing we can easily debug it.
if (file_exists($file)) die("Missing required file {$file}");

header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));

$debug = '';
while (ob_get_level()) {
    //end all output buffering before file download
    //save this stuff, there could be useful debug information in here.
    $debug .= ob_get_clean();
}

if(!empty($debug)){
    //put "stuff" in a file 
    // -- FILE_APPEND = append to end of log
    // -- lock writing on the file for this opperation
    // --- its possible 2 clients could download the same time.
    file_put_contents(__DIR__.'/download_error.txt', $debug, FILE_APPEND|FILE_EX);
}

//finally output the file
readfile($file);
//this is good
exit;

这样您就可以记录输出而不会弄乱下载。基本上你扔掉了那些东西,它可能是有用的信息。

除此之外,其他一切看起来都不错,我的意思是没有太多可能出错的地方。如果我知道garbage code.是什么,也许我可以帮助更多。也许原始文件不好,谁知道......

我无法肯定地说,任何这一点都可以解决你的问题,我提出的大部分内容只是我个人对日志记录和执行顺序的偏好。但它可能不会解决它。

安全

作为最后一点,我应该提到你应该非常小心这个?file=TEST.zip恶意​​用户可以做到这一点。

 ?file=../../../../etc/passwd  //which is a bad example (unless PHP is admin)

基本上,他们可以使用您的输入来横向目录结构。更好的方法就是这样做

 $file = false;
 switch($_GET['file']){
      case 'TEST.zip': //you can even use a hash 
          $file = 'TEST.zip';
      break;

      //you can even use a hash instead of the name
      //this prevents errors caused by things like quotes in the filename.
      //something as simple as switch(md5($_GET['file'])), is finr
      case 'uiOzAWe8aser':
          //then the user has no access to your filesystem
         $file = 'TEST.zip';
      break;
      default: die('File not found '.$_GET['file'];
 }

当然,在许多情况下这可能不实用,但您可以在数据库等中进行验证。

© www.soinside.com 2019 - 2024. All rights reserved.