[SOLVED] Downloading Files with PHP header()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drgnjucr
Forum Commoner
Posts: 30
Joined: Thu Jan 27, 2005 3:06 pm
Location: NJ
Contact:

[SOLVED] Downloading Files with PHP header()

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how is $type set?
drgnjucr
Forum Commoner
Posts: 30
Joined: Thu Jan 27, 2005 3:06 pm
Location: NJ
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how does your upload move the files? Where's the "size" coming from? Should probably post your upload processing function(s).
drgnjucr
Forum Commoner
Posts: 30
Joined: Thu Jan 27, 2005 3:06 pm
Location: NJ
Contact:

Post 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'];
drgnjucr
Forum Commoner
Posts: 30
Joined: Thu Jan 27, 2005 3:06 pm
Location: NJ
Contact:

Post 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..
drgnjucr
Forum Commoner
Posts: 30
Joined: Thu Jan 27, 2005 3:06 pm
Location: NJ
Contact:

[SOLVED]

Post 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.
php_hacker
Forum Newbie
Posts: 7
Joined: Mon Mar 07, 2005 3:44 pm

Post 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.
Post Reply