file download through php

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a problem when doing indirect download of file through php, when
I click on download link Firefox and Internet Explorer give me same
options: Open and Save. Firefox opens file directly from Internet and
downloads without problem.

Internet Explorer saves file on local disk and I can open it from disk
correctly but when I try to open it directly from Internet it responds
with "There was an error opening this document. This file cannot be
found.", concrete situation in my usage is Pdf document. Below is my
concrete code, any suggestions are appreciated.

ini_set('display_errors', 'Off');

$args = trim($_SERVER["PATH_INFO"]);
$args = preg_replace("/^\//i", '', $args);
$argv = explode('/', $args);
$id = (int)$argv[0];

$f = fm_file::instance($id);

if($f->get('id') < 1) {
    die('No file.');
header('Content-Disposition: attachment');
header('Accept-Charset: UTF-8');
header('Content-Type: '.$f->get('type').'; charset="UTF-8";');

Re: file download through php wrote:
Quoted text here. Click to load it

OK, I can see several possible issues here:

Quoted text here. Click to load it

I think you're supposed to specify a file name here:

header("Content-Disposition: attachment;

Quoted text here. Click to load it

I am not sure "Accept-Charset:" header is appropriate when serving
binary files...  Comment this line out and see what happens...

Quoted text here. Click to load it

I am not sure that "charset" field is appropriate within
"Content-Type:" header when the content type it not "text/*".  In any
case, there should be no semicolon at the end of a header...  Try
changing it to this:

header('Content-Type: ' . $f->get('type'));


Re: file download through php


Quoted text here. Click to load it

  RFC2183 says the filename parameter is optional, though whether that
has any bearing on what you are supposed to include in the header
fields of an HTTP message is another question.

Quoted text here. Click to load it

  Since Accept-Charset is defined only as a request-header, it is
meaningless in a response (unless there is a private agreement which
assigns it a meaning).


Re: file download through php wrote:
Quoted text here. Click to load it

The problem you encountered has to do with the dumb way IE's caching
mechanism works. If it receives a no-cache directive from the server,
IE would immediately remove the file after it's downloaded. The
external program doesn't get a chance to open it.

Add this and see if that fixes the problem:

header('Cache-Control: ');

Re: file download through php

Chung Leong wrote:
Quoted text here. Click to load it

I am wrestling with a very similar problem. Here is some of my php
script (after taking the
above suggestion on board):

Header("Content-Type: application/octet-stream ");
Header("Content-Disposition: attachment; filename=\"$filename\"");
Header("Content-Length: $length");
Header("Cache-Control: ");

while (@ob_end_flush());

function clip_tv() {
  global $startTime, $duration, $fileLocation, $bitrate;
  $cutterCommand = "/home/fedora/bin/mencoder 3>&1 1>&2  -quiet -sb
$startBytes -endpos    $duration -oac copy -ovc copy -of mpeg
$fileLocation -o /proc/self/fd/3 2>/dev/null";

I (left-) click on a link which calls this script with some request
parameters. IE pops up
a box asking me to open or save. I select "save" and browse to a file.
IE downloads until
it reaches, supposedly, 99% of the total stream length and then "Cannot
copy file. Source
file or disk is unreadable" (my translation).

Firefox is no problem. Incidentally, IE also ignores the suggested
filename in the Content-disposition header.

Colin Rosenthal

Re: file download through php

"IE also ignores the suggested filename in the Content-disposition
header" and also Safari will not properly display non latin1 charsets.
my solution for download link:
URL: /$file_id/$file_name
1. download.php can be replaced using mod_rewrite into something more
2. $file_id can be location of file or anything else
3. $file_name is used only to indicate filename to browser and can be
string with any character and IE,Firefox and Safari will display
properly filename for download

and about IE strange problem? would like to know how to fix it but
because i have no time I've gone with "inline" instead "attachment" for

Re: file download through php

For IE is needed:

header("Pragma: public");


Site Timeline