【问题标题】:php image file download by codephp图片文件代码下载
【发布时间】:2011-08-12 01:00:49
【问题描述】:

这涉及使用 PHP 下载文件

我的php版本是5.3.5,我的apache是​​2.2.17

我正在尝试下载我在服务器中上传的文件(pdf、jpg、tiff),它们以相同的大小和类型下载,但我看不到它们。我猜他们没有被正确复制。但是当我打开原始上传的那些时,它们工作得很好。 我已经看到了几乎所有按照建议出现的问题,但没有一个人回答了这个问题,只有一个类似的问题是this,但仍然没有回答我的问题。

下载我正在使用此代码

       header("Content-type: application/force-download"); 
       header('Content-Disposition: inline; filename="' . $dir . '"'); 
       header("Content-Transfer-Encoding: Binary"); 
       header("Cache-Control: no-store, no-cache, must-revalidate");  
       header("Cache-Control: post-check=0, pre-check=0", false);  
       header("Pragma: no-cache"); 
       header("Content-length: ".filesize($dir)); 
       header('Content-Type: application/octet-stream'); 
       header('Content-Disposition: attachment; filename="' . $file . '"'); 
       readfile("$dir"); 

$dir="62756d616769636e63/646973736572746174/ehddggh/1.JPG"$file="1.JPG"

谁能给我一个提示我做错了什么,或者给我一个更好的解决方案来下载文件?

【问题讨论】:

  • Content-type 两次... Content-disposition 两次... 第一个标头不会做任何事情(不会被发送)。还有filesize($dir) 有点奇怪,你不觉得吗? dirfile 大小...

标签: php image file download


【解决方案1】:

这听起来像是您在下载的文件中获得了额外的(虚假)内容。

确保文件中的 PHP 打开标记之前没有 BOM 标头、空格或其他任何内容;此外,您在关闭 PHP 标记后没有尾随空格或任何其他数据(如果您关闭 PHP 标记)。

另外,稍微清理一下您的代码:为什么会有多个 Content-Type 标头?为什么有多个 Content-Disposition 标头?

【讨论】:

    【解决方案2】:
    readfile($dir); // without the quotes?
    

    另外,确保 $dir 确实存在

    is_file($dir)file_exists($dir)

    【讨论】:

    • 虽然不带引号写起来确实更简洁,但这不是他的问题的原因
    • 删除所有这些标题,看看你是否得到任何输出。
    【解决方案3】:

    谢谢大家的回答。我将下载作为文件中的一个函数调用,其中包含其他函数,所以最后我不得不单独编写一个脚本,除了其他文件。我的问题是我需要它是安全的,并且仅在文件属于用户并且用户已登录时才下载文件,因此我发送了我需要的所有数据,加密并在脚本内部我使用了一系列的东西来看看所有者是否真的是登录用户。因此,如果有人想知道这是我使用的代码并且可以完美运行。

    <?php
    session_start();
    $a=$_GET['a'];
    $parts=explode("-",$a);
    $tres=$parts[0];
    $nombre=$partes[1];
    $dbcodletra=substr($tres,2);
    if($dbcod!=$_SESSION["username"])$boolt=0;
    if($ext==1) $extl=".jpg";
    if($ext==2) $extl=".jpeg";
    if($ext==3) $extl=".tif";
    if($ext==4) $extl=".tiff";
    if($ext==5) $extl=".pdf";
    if($ext==6) $extl=".doc";
    if($ext==7) $extl=".docx";
    if($tipoproy==1) $dir="rute/".$dbcodletra."/".$nombre.$extl;
    if($tipoproy==2) $dir="rute/".$dbcodletra."/".$nombre.$extl;
    if($tipoproy==3) $dir="rute/".$dbcodletra."/".$nombre.$extl;
    if($tipoproy==4) $dir="rute/".$dbcodletra."/".$nombre.$extl;
    if (file_exists($dir) && $boolt) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($dir));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($dir));
        ob_clean();
        flush();
        readfile($dir);
        exit;
    }else echo "<meta http-equiv=\"Refresh\" content=\"0;url=misdocumentos.php\">";
    ?>
    

    【讨论】:

      【解决方案4】:

      在一个两年前的问题上支付它......

      我遇到了无法打开的损坏下载的类似问题(当右键单击和另存为完美工作时)。阅读@Jon's 的答案后,我认为他正在做某事。如果您查看 readfile 的文档(链接如下),您将在他们的示例中看到 ob_clean()、flush() 和 exit。所有这些都将最大限度地减少响应中额外字符数据的泄漏。我刚刚复制了他们的示例 #1,我的问题就解决了。

      http://php.net/readfile

      【讨论】:

        【解决方案5】:

        您的标题看起来很乱,尝试这样做:

        header('Pragma: public');
        header('Cache-Control: public, no-cache');
        header('Content-Type: application/octet-stream');
        header('Content-Length: ' . filesize($dir));
        header('Content-Disposition: attachment; filename="' . basename($dir) . '"');
        header('Content-Transfer-Encoding: binary');
        
        readfile($dir);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多