Page 1 of 1

[SOLVED] Downloading Files with PHP header()

Posted: Mon Mar 07, 2005 11:19 am
by drgnjucr
I'm having difficulty downloading files on my server. For some reason, when attempting to DL files other than text files, the file gets corrupted.
Adobe reports the file being corrupted or something, Microsoft word and excel reference a missing file, with is my .css file and the the doc is unreadable.

Here is my code for the header download of the file:

Code: Select all

$theFile = "$path" . "$name"; # combine the path and file 
								header("Cache-Control: ");# leave blank to avoid IE errors
								header("Pragma: ");# leave blank to avoid IE errors
								header("Content-type: $type");
								header("Content-Disposition: attachment; filename="".$name.""");
								header("Content-length: $size");
								readfile($theFile);

Anyone else have this problem, or know how to fix it?
Thanks!

Posted: Mon Mar 07, 2005 11:20 am
by feyd
how is $type set?

Posted: Mon Mar 07, 2005 11:27 am
by drgnjucr
I'm storing type in a MySQL db..

application/msword
application/vnd.ms-excel
application/pdf
text/plain

I'm storing the path,filename,size, along with some other irrelaveant details in the db.

Posted: Mon Mar 07, 2005 11:37 am
by feyd
how does your upload move the files? Where's the "size" coming from? Should probably post your upload processing function(s).

Posted: Mon Mar 07, 2005 11:41 am
by drgnjucr
Well, I will post. But I did forget to mention that currently I am using Windows as my test server and if I navigate to the upload directory I can open them up no problem. It's only when I attempt to DL via a script.

Do you still want my code?

This is how I get the size:

Code: Select all

$_FILESї'userfile']ї'size'];
I'm using:

Code: Select all

move_uploaded_file($tmpName, $uploadfile)
to upload the file.

Here are my vars:

Code: Select all

$fileName = $_FILESї'userfile']ї'name'];
				$tmpName = $_FILESї'userfile']ї'tmp_name'];
				$fileSize = $_FILESї'userfile']ї'size'];
				$fileType = $_FILESї'userfile']ї'type'];

Posted: Mon Mar 07, 2005 1:16 pm
by drgnjucr
Ok, so none of the files work. They seem open the contents of page source??

The text files, are the current page source. I believe that's what is happeneing to all of the files..

[SOLVED]

Posted: Mon Mar 07, 2005 2:54 pm
by drgnjucr
I'm not excatly sure what fixed this, but it's fixed. I kept messin around with it and it worked.
I can't figure out where I went wrong, but I'm no longer wrong.

Thanks.

Posted: Mon Mar 07, 2005 4:14 pm
by php_hacker
drgnjucr, I use this code to download files directly to my server.
And it rocks for me...

Code: Select all

$url=$_POSTї'url1'];
$file=$_POSTї'name'];
$fp=fopen($file,"a"); 
require_once "Request.php";
$req =& new HTTP_Request($url);
$req->addHeader("User-Agent","Mozilla/4.0")");
if (!PEAR::isError($req->sendRequest())) {
$max=$req->getResponseBody();
fwrite($fp,$max);
}
fclose($fp);
echo "ok";
You will need Request library for it.