Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

File Downloads through a PHP Script

5:12 pm on Feb 14, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 14, 2002
votes: 0

OK. Here's what I want to do.

Firstly, I'm inside a script using PHP sessions (according to the fpasshtru manual page this can make a difference).

I want to have people click on a download link. When they do, I execute a bit of code, then send them the file.

Firstly, I tried using the Location: header. This worked fine in IE, but Mozilla just displayed the file (think I probably have a nextraneous space or line reuturn somewhere - haven't been able to find it - my script is in multiple files).

I have now got it so I can download the file, but the file is given the name of the PHP script, which I don't want. I want the file to have it's original filename.

Here's my code so far:

$filename = 'files/' . $data_upload['filename'];

if (file_exists ($filename)) {

$FILECMD = '/usr/bin/file';
$contentType = '';
$fp = popen("$FILECMD -bin $filename", 'r');

if (!$fp) {
} else {
while($string=fgets($fp, 1024)) $contentType .= $string;

// IE cannot download from sessions without a cache
header('Cache-Control: public');
// Fix problems with multiple .'s in filenames
$fileName = preg_replace('/\./', '%2e', $fileName, substr_count($fileName, '.') - 1);

header("Content-type: $contentType");
header("Content-Disposition:inline; filename=\"".$file."\"");

while(!feof ($fd)) {
print fread($fd, 4096);

} else {
print "File Not Found";

Please tell me this can be done!


PS. Someone should really fix these forums to preserve code indenting o_O

11:12 am on Feb 17, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 14, 2002
votes: 0

I considered deleting the post, but I thought I'd reply instead.

I'm thick.

I uploaded a binary file in ascii mode, and that's what appears to have caused the problem. Both the above code and mozilla now work.

<bangs head against monitor>


2:39 pm on Feb 17, 2003 (gmt 0)


WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
votes: 0

I couldn't see it either so I am glad you found the answer. It was bugging me. :)

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members