通过php脚本下载文件


Downloading File via php script

我刚刚从000webhost(免费)切换到godaddy(付费)服务器,到目前为止,它比000webhost差了10倍。>

无论如何,我的问题是下载。我使用了以下代码,它在000webhost上运行良好,允许用户下载rar文件,但现在在godaddy上,rar文件会返回存档结束错误。我通过FTP检查,文件没有通过上传损坏

<?php
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$IP1 = $_SERVER['HTTP_X_FORWARDED_FOR'];
$IP2 = $_SERVER['REMOTE_ADDR'];
$HWID = $_GET['HWID'];
date_default_timezone_set('America/New_York');
$date = date("m/d/y g:i:sA", time());
$file = $_GET['file'];
file_put_contents('Logs/Downloaded.txt', "IP: " . $IP2 . " Downloaded On: " . $date . " File: " . $file . " User Agent: " . $userAgent . "'n", FILE_APPEND);

$path = "files/";
$fullPath = $path.$file;
if(file_exists($fullPath)){
if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        break;
        case "zip":
        header("Content-type: application/zip");
    break;
        default;
        header("Content-type: application/octet-stream");
    }
    header("Content-Disposition: attachment; filename='"".$path_parts["basename"]."'"");
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    ob_flush();
        flush(); 
    }
}
fclose ($fd);
exit;
}else{
    die("File could not be found");
}
?>

脚本完成了吗?您可能需要set_time_limit(0)来确保它完成。

另一件事是在十六进制编辑器中打开下载的文件,查找异常情况——我怀疑你可能有PHP警告。您在输出循环中调用ob_flush,但据我所知,您没有执行任何输出缓冲。这可能会触发警告,破坏您的输出。