简体   繁体   中英

Downloading File via php script

I just switched from 000webhost(free) to godaddy(paid) servers and so far, it's like 10x worse than 000webhost >.>

Anyways, my problem is with the downloads. I use the following code and it worked fine on 000webhost to allow users to download rar files but now on godaddy, the rar file would return a end of archive error. I checked via FTP, the files are have not been corrupted via upload

<?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");
}
?>

Does the script complete? You might need set_time_limit(0) to ensure it completes.

The other thing would be open the downloaded file in a hex editor and look for anomalies - I suspect you might have a PHP warning in there. You are calling ob_flush in your output loop, but as far as I can tell you're not performing any output buffering. It's possible that is triggering a warning, corrupting your output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM