【问题标题】:try to download file and getting invalid file in response in core php尝试在核心 php 中下载文件并获取无效文件作为响应
【发布时间】:2012-09-22 08:36:45
【问题描述】:

我下载了一个文件,但它返回的文件无效。

这是我的download_content.php

<?php    
  $filename = $_GET["filename"]; 


    $buffer = file_get_contents($filename);

    /* Force download dialog... */
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    /* Don't allow caching... */
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

    /* Set data type, size and filename */
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . strlen($buffer));
    header("Content-Disposition: attachment; filename=$filename");

    /* Send our file... */
   echo $buffer; 
?> 

下载文件链接:

<a href="download_content.php?filename=/gallery/downloads/poster/large/'.$r['file'].'"> Download</a>

$r['file'] 包含要下载的文件名。

文件所在文件夹的完整路径为:

localhost/ja/gallery/downloads/poster/large/'.$r['file'].'

jahtdocs 中的根文件夹。

我不知道真正的问题是什么,谁能帮帮我吗?

【问题讨论】:

    标签: php core


    【解决方案1】:
        <?php
        header( "Content-Type: application/vnd.ms-excel" );
        header( "Content-disposition: attachment; filename=spreadsheet.xls" );
    
        // print your data here. note the following:
        // - cells/columns are separated by tabs ("\t")
        // - rows are separated by newlines ("\n")
    
        // for example:
        echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
        echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n";
       ?>
    

    【讨论】:

      【解决方案2】:

      正如另一个问题中所说,这种方式看起来更好:

      $filename = $_GET["filename"];
      // Validate the filename (You so don't want people to be able to download
      // EVERYTHING from your site...)
      
      // For example let's say that you hold all your files in a "download" directory
      // in your website root, with an .htaccess to deny direct download of files.
      // Then:
      
      $filename = './download' . ($basename = basename($filename));
      
      if (!file_exists($filename))
      {
          header('HTTP/1.0 404 Not Found');
          die();
      }
      // A check of filemtime and IMS/304 management would be good here
      // Google 'If-Modified-Since', 'If-None-Match', 'ETag' with 'PHP'
      
      // Be sure to disable buffer management if needed
      while (ob_get_level()) {
         ob_end_clean();
      }
      
      Header('Content-Type: application/download');
      Header("Content-Disposition: attachment; filename=\"{$basename}\"");
      header('Content-Transfer-Encoding: binary'); // Not really needed
      Header('Content-Length: ' . filesize($filename));
      Header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      
      readfile($filename);
      

      也就是说,“无效文件”是什么意思?长度不好?零长度?文件名不好? MIME 类型错误?文件内容错误?看在眼里的一切,你可能清楚其中的含义,但在我们看来,这远非显而易见。

      更新:显然找不到文件,这意味着 PHP 脚本的 filename= 参数错误(指的是不存在的文件)。修改了上面的代码,允许一个目录包含所有文件,并从那里下载。

      【讨论】:

      • 这对我帮助很大。谢谢
      【解决方案3】:

      您的 $filename 变量包含如下完整路径

       header("Content-Disposition: attachment; filename=$filename");
      

      这样做

      $newfilename = explode("/",$filename);
      $newfilename = $newfilename[count($newfilename)-1];
      
      $fsize = filesize($filename);
      
      Then pass new variable into header
      
      header("Content-Disposition: attachment; filename=".$newfilename);
      header("Content-length: $fsize");
      
      //newline added as below
      ob_clean();
      flush();
      readfile($filename);
      

      【讨论】:

      • 你能粘贴整个错误,你到底得到了什么?您是否将文件内容放入缓冲区变量中?
      • 是的,我在 $filename 中获取文件的完整路径.. 但是当我们点击下载链接时,它返回一个无效的图像......实际图像大小为 459kb,但它给出了 420byte 的文件作为回报......
      • 你的路径是绝对的,所以不要使用 file_get_contents。查看我的更新答案
      • 仍然得到相同的结果无效文件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多