Lassie Posted December 22, 2006 Share Posted December 22, 2006 I am trying to build a download script and currently have a couple of building blocks to see how things work.The following two scripts are concerned with fetching a file and creating a download alert.At present when the link is activated all i get is a blank browser when I am expecting a windows alert to appear giving the chance to download or open the file.Can anyone tell me where I am going wrong?promt.php[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Prompt for download</title></head><body><p><a href="test.php?file=test.txt">Download text 1</a></p><p><a href="test.php?file=10steps.jpg">Download image 2</a> </p></body></html>[/code]test.php[code]<?php$nogo = 'Sorry, download unavailable. <a href="prompt.php">Back</a>.';$getfile = ($GET['file']);$filepath = 'C:/Program Files/EasyPHP1-8/www/phpsolutions/images/'.$getfile;// check that it exists and is readableif (file_exists($filepath) && is_readable($filepath)) {// get the file's size and send the appropriate headers$size = filesize($filepath);header('Content-Type: application/octet-stream');header('Content-Length: '.$size);header('Content-Disposition: attachment; filename='.$getfile);header('Content-Transfer-Encoding: binary');// open the file in binary read-only mode// suppress error messages if the file can't be opened$file = fopen($filepath, 'rb');if ($file) {// stream the file and exit the script when completefpassthru($file);exit; }else {echo $nogo; } ?>[/code] Link to comment https://forums.phpfreaks.com/topic/31628-download-script/ Share on other sites More sharing options...
Orio Posted December 22, 2006 Share Posted December 22, 2006 This is what I use, and works great with FF and IE:[code]<?php//$file name holds the filenameif(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');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: application/octet-stream");header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );header("Content-Transfer-Encoding: binary");header("Content-Length: ".filesize($filename));@readfile($filename);exit;?>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/31628-download-script/#findComment-146607 Share on other sites More sharing options...
Lassie Posted December 23, 2006 Author Share Posted December 23, 2006 Hi,Thanks for that.If possible could you provide a few words of explanation.Specifically, What does the if statement mean/do?What is the difference betweenreadfile and fpassthru?Would I expect to see a windows alert to save or download the file?Thanks,lassie Link to comment https://forums.phpfreaks.com/topic/31628-download-script/#findComment-146932 Share on other sites More sharing options...
Orio Posted December 23, 2006 Share Posted December 23, 2006 zlib.output_compression (as you can read [url=http://www.php.net/manual/en/ref.zlib.php#ini.zlib.output-compression]here[/url]) compresses in some sort of way the data sent to the browser. All I know that when dealing with "force downloads", some browsers don't like it so you need to turn it off (temporarily of course, it will be changed back to the original value when the script ends).First four headers are about caching, you can read about caching [url=http://www.mnot.net/cache_docs/]here[/url]. Some browsers require certain caching headers to accept the download.The last four headers are for the download itself. You have included these headers yourself in your original script, so I guess no further explanations needed.[url=http://www.php.net/manual/en/function.readfile.php]readfile()[/url] outputs all the data in a file. [url=http://www.php.net/manual/en/function.fpassthru.php]fpassthru()[/url] outputs a file from the location of it's internal pointer to the end of the file, using a handle (that means you need to use fopen()). Since you haven't changed the place of the internal pointer in your script, there will be no difference between readfile() and fpassthru(), except the fact you need to use fopen() to open the file first when using fpassthru().Orio. Link to comment https://forums.phpfreaks.com/topic/31628-download-script/#findComment-146957 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.