Jump to content

Sending files to browser.


flyhoney

Recommended Posts

I want a user to be able to download a .zip file from my server, without actually hardlinking to the file. Ive seen this done, I just dont know how to do it.  I want a user to be given a link to something like:

 

download.php?file=345342343

 

and have that script send the file to them.  Ive tried something to the effect of:

header("Content-type: application/zip");
$f = fopen(yada yada);
while( $f != eof ){
echo yadayada;
}

 

But that didn't seem to work, any ideas?

 

 

Link to comment
https://forums.phpfreaks.com/topic/49595-sending-files-to-browser/
Share on other sites

<?php

$filename=$_REQUEST['filename'];
$filename = realpath($filename);

$file_extension = strtolower(substr(strrchr($filename,"."),1));

/****Set Header Content Type based on File Extension****/      
        switch ($file_extension) {
               case "zip": $ctype="zip"; break;
               default: $ctype="application/force-download";
           }
           if (!file_exists($filename)) {
               die("NO FILE HERE");
           }

    header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private",false);
            header("Content-Type: $ctype");
            header('Content-Description: File Transfer'); 
            header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: ".filesize($filename));
            set_time_limit(0);
            @readfile("$filename") or die("File not found."); 

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.