Previous Thread
Next Thread
Print Thread
Rate Thread
Joined: May 2002
Posts: 12
Newbie
Newbie
Offline
Joined: May 2002
Posts: 12
I am using a script like given below to open files stored *outside* the webroot directory for users to download. The problem is whenever the filesize exceeds 400,000 bytes, this script cannot open the file. It returns a blank page

Any idea what I shall do to get rid of this??


<?
define('FILEDIR', '/home/myhotboard.com/downloads/');
//storage directory outside the root
$path = FILEDIR . $file;

$mimetype = array(
'doc'=>'application/msword',
'htm'=>'text/html',
'html'=>'text/html',
'jpg'=>'image/jpeg',
'pdf'=>'application/pdf',
'txt'=>'text/plain',
'xls'=>'application/vnd.ms-excel'
);

$p = explode('.', $file);
$pc = count($p);



header("Content-type: application/force-download\n");
header("Content-disposition: attachment; filename="$file"\n");
header("Content-transfer-encoding: binary\n");
header("Content-length: " . filesize($path) . "\n");

//send file contents
$fp=fopen($path, "r");
fpassthru($fp);
?>

Sponsored Links
Joined: Apr 2002
Posts: 1,768
Addict
Addict
Offline
Joined: Apr 2002
Posts: 1,768
One thing I would change is to use a loop, rather than reading the whole file into memory, as fpassthru probably does:

Code
//send file contents <br />(($fp = fopen($path, 'rb')) !== FALSE) or die("fopen: failed to open '$file'\n"); <br />while (!feof($fp)) { <br />   $buf = fread($fp, 16384); <br />   fwrite($fp, $buf); <br />} <br />fclose($fp);


The buffer size of 16384 (16K) is arbitrary. You could make it larger or smaller.

It's also a good idea to use the 'b' (binary) flag as above, so that the script will work on both Unix and Windows platforms.

Last edited by Dave_L; 05/25/2003 5:53 AM.
Joined: May 2002
Posts: 12
Newbie
Newbie
Offline
Joined: May 2002
Posts: 12
So the final code shall look like?


//send file contents
$fp=fopen($path, "rb");
(($fp = fopen($path, 'rb')) !== FALSE) or die("fopen: failed to open '$file'\n");
while (!feof($fp)) {
$buf = fread($fp, 32768);
fwrite($fp, $buf);
}
fpassthru($fp);
fclose($fp);
?>


Joined: Apr 2002
Posts: 1,768
Addict
Addict
Offline
Joined: Apr 2002
Posts: 1,768
The code I posted would replace your code, so it should look like:

Code
//send file contents<br />(($fp = fopen($path, 'rb')) !== FALSE) or die("fopen: failed to open '$file'\n");<br />while (!feof($fp)) {<br />   $buf = fread($fp, 32768);<br />   fwrite($fp, $buf);<br />}<br />fclose($fp);<br />?>



Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
isaac
isaac
California
Posts: 1,157
Joined: July 2001
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20221218)