PHP文件下载一直是腐败的(50%的时间) [英] PHP File Download Is Always Corrupt (50% of the time)

查看:113
本文介绍了PHP文件下载一直是腐败的(50%的时间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自动下载文件的脚本。
它完美地下载文件,但问题是50%或更多的时间,它下载一个损坏的文件。



通常删除和下载再次工作,但不总是。



如何让这个下载完全永远是完美的,没有损坏?
文件大小根据正在下载的文件而有所不同。

 <?php 

//自动启动文件下载
if(isset($ _ GET ['filename'])):
$ filename = $ _GET ['filename'];
$ domain =http://www.domain.com;
$ filepath =/ addons / downloads / websites /;

//构建文件信息
$ file = $ domain。 $ filepath。 $文件名;
// echo $ filepath。 $文件名;
// echo $ file;

// CREATE / OUTPUT THE HEADER
if(file_exists(/ home / unrealde / public_html / ebook / domain.com /\".$ filepath。$ filename)):
标题('内容描述:文件传输');
header('Content-Type:application / octet-stream');
header('Content-Disposition:attachment; filename ='。basename($ file));
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($ file));
ob_clean();
flush();
readfile($ file);

else:
$ errorMsg =< b>下载错误:文件$ filename不存在!< / b>< br />请联系< a href =的mailto:support@domain.com'> support@domain.com< / A>中;
endif;

echo $ errorMsg;

else:
//不要下载任何文件
endif;
?>


解决方案

我的预感是,程序中的某些东西正在输出一些数据不是文件本身。



您是否在二进制编辑器中查看了损坏的文件,并将其与未损坏的版本进行了比较?你会发现,无论是在文件的开头还是结尾,你都有一些意想不到的数据,这是什么是破坏文件。



如果你以这种方式看这个文件,这可能会变得非常明显是什么问题。例如,您可能拥有该文件,然后出现错误消息,在这种情况下,您的行可能是 echo $ errorMsg; 是罪魁祸首。



或者你可能有一些空格。这也可能是相同的错误消息,或者可能是您的PHP标签上面或下面的空白行正在打印。



我的第一个建议是因为程序在输出文件时有效地完成,在 readfile(); 函数; 函数$ c>行。一旦发送文件,这将彻底阻止任何进一步的虚假数据输出。



如果在<$ c $之前发送坏数据,这将不会有帮助c> readfile(); ,但它排除了一半可能的问题。


I have a script which automatically downloads a file. It works perfectly to download the file, but the problem is that 50% or more of the time, it downloads a corrupt file.

Usually deleting and downloading again works, but not always.

How can I make this download 100% of the time perfectly always, not corrupted? The file size changes depending on the file being downloaded.

<?php

// Automatically Start File Download 
if (isset($_GET['filename'])):
    $filename = $_GET['filename'];
    $domain = "http://www.domain.com";
    $filepath = "/addons/downloads/websites/";

     //BUILD THE FILE INFORMATION
     $file = $domain . $filepath . $filename;
     // echo $filepath . $filename;
     // echo $file;

     //CREATE/OUTPUT THE HEADER
    if (file_exists("/home/unrealde/public_html/ebook/domain.com/".$filepath . $filename)):
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        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($file));
        ob_clean();
        flush();
        readfile($file);

    else: 
        $errorMsg = "<b>Download Error: File $filename Doesnt Exist!</b> <br />Please Contact <a href='mailto:support@domain.com'>support@domain.com</a>";
    endif;

    echo $errorMsg;

else: 
    // don't download any file
endif;
?>

解决方案

My hunch is that something in your program is outputting some data other than the file itself.

Have you looked at the corrupt file in a binary editor and compared it with a non-corrupt version? What you'll find is that either at the beginning or the end of the file, you have some unexpected data, and this is what is corrupting the file.

If you look that file this way, it may become very obvious what the problem is. For example, you may have the file, followed by an error message, in which case maybe your line echo $errorMsg; is the culprit.

Alternatively you may have some blank space. This could also be the same error message, or it could be that your PHP tags have blank lines above or below them, which are being printed.

My first suggestion would be, since the program is effectively finished when the file is output, to put an explicit die; function immediately after the readfile(); line. This will categorically prevent any further spurious data being output once the file has been sent.

That won't help if the bad data is being sent before the readfile();, but it does rule out half the possible problems in one swoop.

这篇关于PHP文件下载一直是腐败的(50%的时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆