Welcome to WebmasterWorld Guest from 18.232.124.77

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
posts:118
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) {
$contentType='application/octet-stream';
} else {
while($string=fgets($fp, 1024)) $contentType .= $string;
pclose($fp);
}

if (strpos ($HTTP_SERVER_VARS['HTTP_USER_AGENT'], 'MSIE')) {
// 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."\"");
header("Content-length:".(string)(filesize($filename)));
$fd=fopen($filename,'rb');

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

fclose($fd);
} else {
print "File Not Found";
}

Please tell me this can be done!

Allen

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
posts:118
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>

Allen

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

Administrator

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

joined:July 24, 2001
posts:15756
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