Problem with my file download code.... Please Help me!!

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
amita
Forum Newbie
Posts: 3
Joined: Tue Jul 15, 2003 5:13 am

Problem with my file download code.... Please Help me!!

Post by amita »

Hello,

I have written a script to upload file from browser, thats working fine, but I am having lot of problems with my file download code in difference browsers. What I want is the file dialog box that popsup when I click to download a file, I want an "Open" file option also along with the "Save" file option in any of the browsers, which I am not getting. I am pasting my file download code here and also the possible error message that my client is getting. The worst thing is its working from my side but not from the clients side.. Please help this is very critical for my project



Here is the file download code:
header("Content-Type: application/octet-stream;");
header("Content-Disposition: attachment; filename=$row[FileName]");
header("Pragma: no-cache");
header("Expires: 0");

if(file_exists($path.$row[FileName]))
{
$fp=fopen($path.$row[FileName],"rb");
while(!feof($fp))
{
$fcontents=fread($fp,4096);
echo $fcontents;
}
fclose($fp);
}

values in $path and $row[FileName] are correct.



Here is the clients list of error messages:
1. For MS Word Document, I got "This file can not be found. Try one or more of the following: *Check spelling of the name of the document and *Try difference file name."

2. For PowerPoint File, I got: "The path or file name for c:\Documents and Setting\tan lam\local setting\temporary Internet files\Content .IE5\G… is not valid. Please check the path and file name are correct.

3. For Flash File: I got Flash Player Open with blank page.

4. For Window Media File, I got "WM Player cannot fine the specified file. Be sure the path is type correctly. If it is , the file does not exist at the specified location, or the computer where the file is stored offline.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

try this code

Code: Select all

<?

	// Get the filename from the query string of the file we want to download
	$fileName = $HTTP_GET_VARS["file"];
	
	// The full path to our downloads directory
	$fileDir = full/server/path/to/the/download/file/";
	
	// Combine the filename and the path
	$path = "$fileDir$fileName";
	
	
	////////////////////////////////////////
	/* Force browser to download the file */
	////////////////////////////////////////

	global $HTTP_USER_AGENT;
	$file = basename($path);
	$size = filesize($path);
	header("Content-Type: application/octet-stream");
	header("Content-Type: application/pdf");
	header("Content-Length: $size");
           
	// IE5.5 just downloads index.php if we don't do this
	if(preg_match("/MSIE 5.5/", $HTTP_USER_AGENT)) {
		 header("Content-Disposition: filename=$file");
	} else {
		header("Content-Disposition: attachment; filename=$file");
	}
    header("Content-Transfer-Encoding: binary");
    $fh = fopen($path, "r");
    fpassthru($fh);

?>
Mark
amita
Forum Newbie
Posts: 3
Joined: Tue Jul 15, 2003 5:13 am

Post by amita »

Thanks. I wil check it with this code.. Hope this works.. !
Bech100 wrote:try this code

Code: Select all

<?

	// Get the filename from the query string of the file we want to download
	$fileName = $HTTP_GET_VARS["file"];
	
	// The full path to our downloads directory
	$fileDir = full/server/path/to/the/download/file/";
	
	// Combine the filename and the path
	$path = "$fileDir$fileName";
	
	
	////////////////////////////////////////
	/* Force browser to download the file */
	////////////////////////////////////////

	global $HTTP_USER_AGENT;
	$file = basename($path);
	$size = filesize($path);
	header("Content-Type: application/octet-stream");
	header("Content-Type: application/pdf");
	header("Content-Length: $size");
           
	// IE5.5 just downloads index.php if we don't do this
	if(preg_match("/MSIE 5.5/", $HTTP_USER_AGENT)) {
		 header("Content-Disposition: filename=$file");
	} else {
		header("Content-Disposition: attachment; filename=$file");
	}
    header("Content-Transfer-Encoding: binary");
    $fh = fopen($path, "r");
    fpassthru($fh);

?>
Mark
Post Reply