Jump to content

Recommended Posts

Hi all. Im writing a web app that requires a file management system. Basically Im giving the users two options, to store to database (mysql 5) or server folder (recommended option).

I have two issues that are doing my head in:

1) When storing to server, upload works correctly. File has unique name I can download from server using Firefox (apache webserver on MacBook pro, with MAMP installed). But, when i install this on a wind 2003 box and IIS6, I get the following error:

 

"Internet Explorer cannot download: http://localhost/view.File.php?file=3"

 

I also noted that when uploading a .doc file to the server, it saved it as application/octet-stream not application/msword. Whats with that? Could be due to ms office not being installed?

 

 

2) Problem number two involves saving to the database. ive changed the mysql max_allowed_packet variable to allow large 16m queries (i doubt it'd ever get that big). Upload is fine and it stores the info, but when I download, all files are corrupted.

 

Ive tried every content-type under the sun and not one combination does the trick. I did a test using the exact code from www.php-mysql-tutorial.com/php-mysql-upload.php and it does the same thing.

 

Has anyone encountered this before?

 

Matt

Link to comment
https://forums.phpfreaks.com/topic/73572-solved-file-download-problems/
Share on other sites

One note, is that the file is downloaded via a new window. eg: <a href="view.File.php?file=3" target="_blank">link</a. So there shouldnt be any problems.

There is no white space before the headers

There is no CSS formatting what so ever.

 

1) Code is very straight forward:

//using mysql functions we get filename, filetype, filesize, filepath, filecontent (mediumblob)

header("Content-Disposition: attachment; filename=".$filename);

header("Content-Length: ".$filesize);

header("Content-Type: ".$filetype);

readfile($filepath);

exit;

 

I have tried also:

header("Content-Disposition: attachment; filename=".$filename);

header("Content-Length: ".$filesize);

header("Content-Type: ".$filetype);

header("Content-Type: application/force-download",false);

readfile($filepath);

exit;

 

Even tried switching the headers around (order wise). No effect.

 

 

2. Code is similar to above, except:

replace readfile with echo $filecontent;

 

It doesnt error out as far as I can tell. MAMP logs show no errors.

 

I will check the IIS logs too.

one thing i know cause some problems

header("Content-Disposition: attachment; filename=".$filename);

should be

header("Content-Disposition: attachment; filename=\"$filename\"");

 

i assume you don't have magic quotes on!

 

change

header("Content-Type: application/force-download",false);

to

header("Content-Type: application/force-download");

 

do you have all the latest patches for IIS ?

Ok, still stuck here. No dice. Need uber help on this.

I have a file stored at:

/Applications/MAMP/htdocs/pmfexecutive/uploads/27596600_1192660820_bg.jpg

 

And I access it using this code:

 

if(file_exists($con->fetchResult(0,"url"))){
$type = $con->fetchResult(0,"filetype");
$size = $con->fetchResult(0,"filesize");
$name = $con->fetchResult(0,"filename");
$path = $con->fetchResult(0,"url");
$handle =fopen($path,'r');
$data = file_get_contents($path);
fclose($handle);
header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
echo $data;
exit;
}

 

Now in theory, this should work on any system yes? Well it fails on IE 6, 7, Firefox, Opera and Safari.

 

In Firefox, it simply echoes the URL:

 

http://localhost:8888/view.File.php?uid=5&table=files

 

Which is of course, the URL to the download files script, telling it to check the files table (i have other tables holding files) opened in a new window.

The database holds the file path to the file mentioned above.

 

Ive tried the header() of force-download and no effect.

 

ANY IDEAS!! >:(

change to

if(file_exists($con->fetchResult(0,"url"))){
$type = $con->fetchResult(0,"filetype");
$size = $con->fetchResult(0,"filesize");
$name = $con->fetchResult(0,"filename");
$path = $con->fetchResult(0,"url");
#$handle =fopen($path,'r');
$data = file_get_contents($path);
#fclose($handle);
echo "$path, $type, $size, $name";

//header("Content-type: $type");
//header("Content-length: $size");
//header("Content-Disposition: attachment; filename=$name");
//header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
//header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
//echo $data;
exit;
}

and post results's (from opening the page directly)

This is what I get as output when I replace my code mate:

http://localhost:8888/view.File.php?uid=5&table=files

Totally strange. It should print out this:

 

/Applications/MAMP/htdocs/uploads/275...bg.jpgimage/jpeg190759bg.jpg

 

Which I get by removing every header and file handle function.

 

I tried to comment out the headers only, and then I had an error of:

unexpected T_STRING in ..

I tracked down the line and this was where the fault was:

//header("Content-Length: $size");
//header("Content-Disposition: attachment; filename=$name"); <--- this is the line number

This is confusing, because (a) the lines are commented out. and (b) they are correct!

 

What THA?

Thought I'd better paste my full view.File.php source in for you:

 

<?php
include("inc.config.php");
// we need to get the information about the file first.
// we can store files in different tables. so it must be specified and ther must be a uid.
$query = "select * from ".$posted['table']." where uid='".$posted['uid']."'";
$dbCon = new DBConnector(); //custom class to get a db connection
$con = $dbCon->getConnection(); 
$result = $con->executeQuery($query);

//if we got a result... 
//get location 'DATABASE' or 'SERVER'	
if($result){
	switch($con->fetchResult(0,"location")){
		case "DATABASE":
			$type = $con->fetchResult(0,"filetype");
			$size = $con->fetchResult(0,"filesize");
			$name = $con->fetchResult(0,"filename");
			$data = $con->fetchResult(0,"content");

			header("Content-type: $type");
		header("Content-length: $size");
		header("Content-Disposition: attachment; filename=$name");
		header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
		echo $data;
 		exit;

		break;
		case "SERVER":
			//read file path from database and spit it out...
			if(file_exists($con->fetchResult(0,"url"))){
				$type = $con->fetchResult(0,"filetype");
				$size = $con->fetchResult(0,"filesize");
				$name = $con->fetchResult(0,"filename");
				$path = $con->fetchResult(0,"url");
				$handle =fopen($path,'r');
				$data = file_get_contents($path);
				fclose($handle);
				header("Content-type: $type");
		             header("Content-length: $size");
		             header("Content-Disposition: attachment; filename=$name");
			header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
		 	echo $data;
 			exit;
			}
			else{
				echo "<p>Could not find the file specified. Check that it exists.</p>";
				echo "<p>File: <i>".$con->fetchResult(0,"url")."<i/></p>";
				exit;
			}
		break;
	}

}


?>

just added a debug for the server,

only other thing i could see was

header("Content-Disposition: attachment; filename=\"$name\"");

<?php
include("inc.config.php");
// we need to get the information about the file first.
// we can store files in different tables. so it must be specified and ther must be a uid.
$query = "select * from ".$posted['table']." where uid='".$posted['uid']."'";
$dbCon = new DBConnector(); //custom class to get a db connection
$con = $dbCon->getConnection(); 
$result = $con->executeQuery($query);

//if we got a result... 
//get location 'DATABASE' or 'SERVER'	
if($result)
{
	switch($con->fetchResult(0,"location")){
		case "DATABASE":
			$type = $con->fetchResult(0,"filetype");
			$size = $con->fetchResult(0,"filesize");
			$name = $con->fetchResult(0,"filename");
			$data = $con->fetchResult(0,"content");
			//Debug
			var_dump($type, $size, $name);
			die;			
			//end Debug
			header("Content-type: $type");
		header("Content-length: $size");
		header("Content-Disposition: attachment; filename=\"$name\"");
		header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
		echo $data;
 		exit;

		break;
		case "SERVER":
			//read file path from database and spit it out...
			if(file_exists($con->fetchResult(0,"url")))
		 {
				$type = $con->fetchResult(0,"filetype");
				$size = $con->fetchResult(0,"filesize");
				$name = $con->fetchResult(0,"filename");
				$path = $con->fetchResult(0,"url");
				#$handle =fopen($path,'r');
				$data = file_get_contents($path);
				#fclose($handle);
				header("Content-type: $type");
            header("Content-length: $size");
            header("Content-Disposition: attachment; filename=\"$name\"");
			header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
		 	echo $data;
 			exit;
			}else{
				echo "<p>Could not find the file specified. Check that it exists.</p>";
				echo "<p>File: <i>".$con->fetchResult(0,"url")."<i/></p>";
				exit;
			}
		break;
	}

}


?>

Ok. I tried that.

Heres a doozy for you. I looked at my MAMP php log file, and it has the following:

 

PHP Fatal Error: Call to undefined function header() in /Applications/MAMP/htdocs/view.File.php on line 57

 

What the hell? Thats a standard PHP function!

  • 1 month later...

Just a little side note on how this was solved.

I found that there were some strange space characters before the start of the header() function. I deleted them and then it worked correctly.

 

Looks like these 'spaces' were stopping the headers from being sent correctly.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.