Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Force Download Causing 0 Byte Files

Tags:

php

download

I'm trying to force download files from my web server using PHP. I'm not a pro in PHP but I just can't seem to get around the problem of files downloading in 0 bytes in size.

CODE:

$filename = "FILENAME...";

header("Content-type: $type");
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
set_time_limit(0);
readfile($file);

Can anybody help? Thanks.

like image 563
Alex Avatar asked Jan 16 '11 15:01

Alex


People also ask

Why is a file downloading as 0 bytes?

Zero byte file may be caused by Incompletely file downloading via Web or file transfer protocol client, or incorrectly transmitting email attachment. Save the file with empty content. Corrupted index table in the file system. Computer cannot handle files or folders with extremely long name.

How do you fix a zero byte output?

Solution 1.Open the Run dialog box by pressing Win and R keys together. Now, open the Command Prompt by typing cmd in the Run dialog box and pressing Enter. At the Command Prompt, type "chkdsk /f e:", where e is the name of the storage device or partition of the hard drive that holds the 0-byte file. Press Enter.

How Force download file from remote server PHP?

Use the readfile() function with application/x-file-to-save Content-type header, to download a ZIP file from remote URL using PHP. header("Content-type: application/x-file-to-save"); header("Content-Disposition: attachment; filename=". basename($remoteURL));


2 Answers

You're not checking that the file exists. Try using this:

$file = 'monkey.gif';

if (file_exists($file))
{
    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);
    exit;
}else
{
    echo "File does not exists";
}

And see what you get.


You should also note that this forces a download as an octet stream, a plain binary file. Some browsers will struggle to understand the exact type of the file. If, for example, you send a GIF with a header of Content-Type: application/octet-stream, then the browser may not treat it like a GIF image. You should add in specific checks to determine what the content type of the file is, and send an appropriate Content-Type header.

like image 128
RobertPitt Avatar answered Sep 27 '22 21:09

RobertPitt


I use the following method in phunction and I haven't had any issues with it so far:

function Download($path, $speed = null)
{
    if (is_file($path) === true)
    {
        $file = @fopen($path, 'rb');
        $speed = (isset($speed) === true) ? round($speed * 1024) : 524288;

        if (is_resource($file) === true)
        {
            set_time_limit(0);
            ignore_user_abort(false);

            while (ob_get_level() > 0)
            {
                ob_end_clean();
            }

            header('Expires: 0');
            header('Pragma: public');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Content-Type: application/octet-stream');
            header('Content-Length: ' . sprintf('%u', filesize($path)));
            header('Content-Disposition: attachment; filename="' . basename($path) . '"');
            header('Content-Transfer-Encoding: binary');

            while (feof($file) !== true)
            {
                echo fread($file, $speed);

                while (ob_get_level() > 0)
                {
                    ob_end_flush();
                }

                flush();
                sleep(1);
            }

            fclose($file);
        }

        exit();
    }

    return false;
}

You can try it simply by doing:

Download('/path/to/file.ext');
like image 42
Alix Axel Avatar answered Sep 27 '22 21:09

Alix Axel