note 39916 added to function.fpassthru

From: Date: Sun, 15 Feb 2004 18:04:05 +0000
Subject: note 39916 added to function.fpassthru
Groups: php.notes 
Request: Send a blank email to php-notes+get-65175@lists.php.net to get a copy of this message
I have also perused this list of examples which I am sure work for that person, but, as others have mentioned here, do not work for me or (anyone else). So what I did was try out all of these examples, check other sources of information, and put together what I think to be an example of what works on 'more than a few' systems. The following example works for me wherever I need to create a download using fpassthru(), which works with IE6 (among other browsers): <? /*/ Download a file using fpassthru() /*/ $fileDir = "/home/pathto/myfiles"; // supply a path name. $fileName = "myfile.zip"; // supply a file name. $fileString=$fileDir.'/'.$fileName; // combine the path and file // translate file name properly for Internet Explorer. if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")){ $fileName = preg_replace('/\./', '%2e', $fileName, substr_count($fileName, '.') - 1); } // make sure the file exists before sending headers if(!$fdl=@fopen($fileString,'r')){ die("Cannot Open File!"); } else { header("Cache-Control: ");// leave blank to avoid IE errors header("Pragma: ");// leave blank to avoid IE errors header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"".$fileName."\""); header("Content-length:".(string)(filesize($fileString))); sleep(1); fpassthru($fdl); } ?> All that should require editing is the $fileDir and $fileName variables. Upload the file and point to it with your browser to see if the script will prompt you for a download. NOTE : Regarding File Types : Leaving the 'Content-type' header as-is should allow you to download pretty much any file. I have tested it on some of the more popular file types including zip, css, php, inc, htm, png, gif and jpg. During these tests, I did note that if I selected 'cancel' or 'open' when prompted to download either a gif or jpg, that it would indeed cancel or open in my image browser as it should, but subsequent attempts at 'downloading only' yielded a web page view of the image. Closing the window and opening a new one reset this, allowing me save a jpeg or gif to the hard drive directly. I believe the problem lies in the way the caching headers are treated, since if any info is specified in the 'cache-control' header, the browser download fails completely (in IE, anyways). Enjoy! Mail me if it works! ;-) ---- Manual Page -- http://www.php.net/manual/en/function.fpassthru.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39916 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39916&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39916&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65175) next »