473,225 Members | 1,328 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

Php Download script problem!

C16
Hi All

I have a simple download script for files on my website and I use the code
below to download the files:

$fp = fopen($filename, "rb");
if ( $fp )
{
$filesize = $file['size'];

header("Cache-Control: post-check=0, pre-check=0");
header("Expires: 0");
header("Content-Type: " . $xtype);
header("Content-Length: " . (string)($filesize));
header("Content-Transfer-Encoding: binary");

while ( !feof($fp) )
{
echo(fgets($fp, 4096));
}
fclose($fp);
}

Now this works just fine on Firefox and Opera, and even on IE when its used
in a web page ie <img src="download.php?id=2"/> but if I try and go to a
file directly by typing in the url www.website.com/download.php?id=2 then it
opens a Download dialog asking me to save the file instead of displaying it
in the browser, this only happens with IE though. Am I missing a vital bit
to the header, or is this a problem with IE, if so is there a way around it
at all?

Many thanks.
Oct 13 '05 #1
4 3593
"C16" wrote:
$fp = fopen($filename, "rb");
if ( $fp )
{
$filesize = $file['size'];

header("Cache-Control: post-check=0, pre-check=0");
header("Expires: 0");
header("Content-Type: " . $xtype);
header("Content-Length: " . (string)($filesize));
header("Content-Transfer-Encoding: binary");

while ( !feof($fp) )
{
echo(fgets($fp, 4096));
}
fclose($fp);
}


Assuming $xtype is defined correctly somewhere, I reckon IE is choking on
the Content-Transfer-Encoding header. This is a MIME header -- it doesn't
belong in an HTTP response at all. Go and read RFC 2616 some time.

You don't need to cast $filesize to a string -- PHP will do this for you
anyway. And while you're at it, I suggest you replace the while() loop with
a readfile() statement instead (<http://uk.php.net/readfile>).

Oh, by the way, filesize() is another useful function. That way you can get
rid of the fopen and fclose instructions altogether.

Phil

--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/
Oct 13 '05 #2
C16
I was using readfile but found a discussion that showed readfile to be '55%'
slower than reading by chunks (in the readfile function desc on php.net
infact). Also tried the header without Content-Transfer-Encoding and many
other permutations from suggestions found via google but still the same
result on IE.

"Philip Ronan" <in*****@invalid.invalid> wrote in message
news:BF743985.39633%in*****@invalid.invalid...
"C16" wrote:
$fp = fopen($filename, "rb");
if ( $fp )
{
$filesize = $file['size'];

header("Cache-Control: post-check=0, pre-check=0");
header("Expires: 0");
header("Content-Type: " . $xtype);
header("Content-Length: " . (string)($filesize));
header("Content-Transfer-Encoding: binary");

while ( !feof($fp) )
{
echo(fgets($fp, 4096));
}
fclose($fp);
}


Assuming $xtype is defined correctly somewhere, I reckon IE is choking on
the Content-Transfer-Encoding header. This is a MIME header -- it doesn't
belong in an HTTP response at all. Go and read RFC 2616 some time.

You don't need to cast $filesize to a string -- PHP will do this for you
anyway. And while you're at it, I suggest you replace the while() loop
with
a readfile() statement instead (<http://uk.php.net/readfile>).

Oh, by the way, filesize() is another useful function. That way you can
get
rid of the fopen and fclose instructions altogether.

Phil

--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/

Oct 13 '05 #3
C16 wrote:
I was using readfile but found a discussion that showed readfile to be '55%'
slower than reading by chunks (in the readfile function desc on php.net
infact). Also tried the header without Content-Transfer-Encoding and many
other permutations from suggestions found via google but still the same
result on IE.

"Philip Ronan" <in*****@invalid.invalid> wrote in message
news:BF743985.39633%in*****@invalid.invalid...

"C16" wrote:
$fp = fopen($filename, "rb");
if ( $fp )
{
$filesize = $file['size'];

header("Cache-Control: post-check=0, pre-check=0");
header("Expires: 0");
header("Content-Type: " . $xtype);
header("Content-Length: " . (string)($filesize));
header("Content-Transfer-Encoding: binary");

while ( !feof($fp) )
{
echo(fgets($fp, 4096));
}
fclose($fp);
}

Assuming $xtype is defined correctly somewhere, I reckon IE is choking on
the Content-Transfer-Encoding header. This is a MIME header -- it doesn't
belong in an HTTP response at all. Go and read RFC 2616 some time.

You don't need to cast $filesize to a string -- PHP will do this for you
anyway. And while you're at it, I suggest you replace the while() loop
with
a readfile() statement instead (<http://uk.php.net/readfile>).

Oh, by the way, filesize() is another useful function. That way you can
get
rid of the fopen and fclose instructions altogether.

Here's what works for me. I haven't tested your case with IE but you
might give it a try.

header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Length: ' . filesize($file));

// to download
header('Content-Disposition: attachment; filename=' . basename($file));

// to open in browser
//header('Content-Disposition: inline; filename=' . basename($file));

readfile($file);

--
*****************************
Chuck Anderson Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Oct 13 '05 #4
C16
Many thanks Chuck that did the trick. For interest the header I have that
works is now:

header("Cache-Control: post-check=0, pre-check=0");
header("Expires: 0");
header('Content-Description: File Transfer');
header("Content-Type: " . $xtype);
header("Content-Length: " . $filesize);
header('Content-Disposition: inline; filename=' . basename($filename));
Here's what works for me. I haven't tested your case with IE but you might
give it a try.

header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Length: ' . filesize($file));

// to download
header('Content-Disposition: attachment; filename=' . basename($file));

// to open in browser
//header('Content-Disposition: inline; filename=' . basename($file));

readfile($file);

--
*****************************
Chuck Anderson Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************

Oct 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rod | last post by:
Hi, I have a script that download any file to the user according to the parameters. I call the script like this: download.php?file=xxx/myfile.doc&file_short=myfile.doc My script is working...
12
by: chipgraphics | last post by:
:confused::confused: I have been on the quest to find a php script that can serve files for downloads and limit the speed at which the file is transfered to the user. I want a faster download...
3
by: Nathan Sokalski | last post by:
I have a webform that contains a button which I want to do three things: 1. Delete a record from a database 2. Let the user download a text file that is generated 3. Refresh the page to show...
5
by: Neil Rossi | last post by:
I have an issue with a particular ASP page on two web servers. Let's call these servers Dev1 and Beta1. Both Servers are running IIS 5, Windows 2000 SP4 with "almost" all of the latest patches. ...
2
by: Jan Paul van de Berg | last post by:
I have a piece of software that people can download and a third party promoting that software. In order for them to be able to count the number of downloads, I have to put a tracking code on my...
12
by: comp.lang.php | last post by:
index.php: // STUFF // STEP 1: imagecreatetruecolor ONLY IF GD 2.0+ SUPPORTED AND FOUND if ($this->isSuccessful && !$hasMogrified && $image && !$newImage &&...
1
by: a.r.austin | last post by:
Hello, I am trying to download a few files one after another from a remote server. Problem is that I don't know how to, or if I am able at all, set a time out for download. I don't want to time...
3
by: malaysiauser | last post by:
Dear all, Last year I install a download control script in Linux server. its working. This year i'd changed my hosting server to other company. i'd tried install the script. It was installed...
1
by: Tim Jones | last post by:
I have a web site where we offer MP3 downloads (yes, they are legal!). I've written a PHP script using readfile() (or fpassthru()) that sends the file using HTTP headers (via various header()...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.