473,757 Members | 3,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)($files ize));
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.p hp?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 3620
"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)($files ize));
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*****@invali d.invalid> wrote in message
news:BF743985.3 9633%in*****@in valid.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)($files ize));
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*****@invali d.invalid> wrote in message
news:BF743985. 39633%in*****@i nvalid.invalid. ..

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

header("Cach e-Control: post-check=0, pre-check=0");
header("Expi res: 0");
header("Cont ent-Type: " . $xtype);
header("Cont ent-Length: " . (string)($files ize));
header("Cont ent-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($filen ame));
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
3208
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 fine except with N4.7 It opens the download box, but the name is download.php instead of myfile.doc
12
26996
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 speed for registered members and a slower speed for guests. The only other thing I can think of is using the header() function to force a download for the requested file type and stream the file to the user's browser. Then using sleep() or usleep()...
3
1645
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 that the record was deleted I am able to do any of these things separately with no trouble. The problem occurs when I try to offer a download AND call my refresh method. When I try to do a download and call my refresh method, only the download is...
5
12308
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. On Beta1, I am able to execute a particular page with no problem, that page opens up in the comes up just fine. On Win2kdev1, when I go to execute the same page, it opens a file download dialog and asks me whether I want to open or save the...
2
2564
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 site. The tracking code must be sent to the client when the user clicks the download button. At the same time, the download must start. The download button links to this page: - Possibility 1, server side redirect to executable...
12
2845
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 && function_exists('imagecreatetruecolor') && preg_match('/2\.0/i', $this->gd_info_array)) { $newImage = @imagecreatetruecolor($configArray, $configArray);
1
3582
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 out whole script, just a part if file won't download in 2mins then skip to the next one. Previously, I had a Javascript implementation with AJAX, this time I thought of doing it in PHP since PHP has far better array functions, I don't have to do...
3
1845
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 properly but doesn't work. More about the script; This script will log date, time, ip address, by day, by month, by year and analysis of downloads. It has 4 log files for one module (year, date...) Now the problem is if i create new module, it...
1
1232
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() directives). The script launches the download fine and the download proceeds normally. However, after 20MB - 35MB of the download completes, the download suddenly completes even though the entire file wasn't download. (The download does not fail.)...
1
47480
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 on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or “save“. I’m going to show you how to do that using a perl script. What You Need Any recent...
0
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.