Hi,

I got some zip files on my server people can download and am having a strange problem.

Can't remember if i ever checked in IE but in FireFox, Google Chrome, Opera everything is fine.

When i download a file from my website in IE the file is getting corrupt.

I extract the files with winzip and some say corrupt/damaged file and some extracts and inside it shows the filename and filetype as file.

I uploaded in binary and via host account, overwritten them to ensure they are not corrupt and still getting the same problem.

I was notified by a user that when it got to 99% in downloading that it would stop and do nothing. I am still awaiting what browser they was using. Thing is i can download them but they seem corrupt or will open but will show a file inside that says it is just a file and windows does not know how to open it.

It seems in all browsers that when i download that the last KB/MB seems to take about 3 seconds to finish like if there is a delay for a second or so although not sure if that is just my virus scanner etc checking the file once downloaded.

Below is the script that i developed some time ago that processes the download.

Can someone tell me if they see something wrong? IE seems to get the file size wrong, if i got a file for example 1.12MB once downloaded it shows as 1.11MB.

I also noticed in Opera that Opera cannot detect the file size at all.

As i say i have not had problems before, i also tried downloading from other sites in IE etc and they downloaded and opened fine. I have noticed that Opera seems to change the file names to download.zip, i don't user Opera so not sure if this is suppose to happen or not. I guess it could be a problem with more than one browser, FireFox, Google Chrome and Opera thou do download them and they open and show as they should. Very confused.

I am pretty desperate to get this sorted.

Thanks
PHPLOVER

<?php	
		
		 
		include ("".$_SERVER['DOCUMENT_ROOT']."/includes/global.php"); // include global file
		require_once("".$_SERVER['DOCUMENT_ROOT']."/includes/session.php"); // include session
		memberonlyzoneerror(); // check if user is logged in
		
		if( isset( $_GET['filename'] ) ) {
			// get filename
			$getfile = $_GET['filename'];
			
			// path to file
			$filepath = "/path/to/file/here/downloads/files/".$getfile."";
			
			// check file exisits, if it does force download/header
			if ( file_exists( $filepath ) ) {
			
			// Headers
			header("Content-type: application/zip");
			header("Content-disposition: attachment; filename=\"".$getfile."");
			header("Content-Transfer-Encoding: binary");
			header("Content-length: " . filesize( $filepath ));
			readfile($filepath);
			
			} else { // else if file does not exist tell them and exit.
				@include ("".$_SERVER['DOCUMENT_ROOT']."/includes/top.inc");
				echo "<h2> File Does Not exist </h2>";
				echo "<p> The file you have requested does not exist. If you feel you received this message in error, please contact us.</p>";
				redirect ("downloads.php", "3");
				include ("".$_SERVER['DOCUMENT_ROOT']."/includes/footer.inc");
				exit;
			}
		} else {
			@include ("".$_SERVER['DOCUMENT_ROOT']."/includes/top.inc");
			echo "<h2> Missing an important parameter </h2>";
			echo "<p>You have used a false link, this page is missing an important parameter. Please report this to Genieuk.</p>";
			redirect ("downloads.php", "8");
			include ("".$_SERVER['DOCUMENT_ROOT']."/includes/footer.inc");
			exit;
		}
	


?>

Thanks

Recommended Answers

All 15 Replies

Hi,

To add to the following, if i download the file directly instead of going through the script above IE downlaods and opens the file fine so must be the script i made above that is causing the problem. Opera also detects download size when downloading directly and not going through the script. Problem is i cannot think what the problem is in the script above, must be because i use FireFox i have never noticed.

No 3 second delay on last bit of download either, user got back to me and they said they are using IE.

Thanks for any help.
PHPLOVER

Hi,

Can someone help me please?

Maybe the headers are wrong way around, really desperate to get this sorted thanks.

PHPLOVER

I found an example that used different headers than yours. You may want to give these a try:

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"'); 
header('Content-Transfer-Encoding: binary');

Hi,

It seems that if i use: header('Content-Type: application/octet-stream'); instead of header('Content-Type: application/zip'); it works. But i don't understand why because it's a zip file so the second header should work fine. Plus i am implementing it in a script i'm building that will replace the content type depending on switch statement so need to see what the problem is still.

There still seems to be a problem with the either the readfile or filesize i think plus the content type i specified should work. Reason why is in Opera it shows as 1.2MB when a file i downloaded was 1.12MB although it still opened fine.

So thanks it works as a temporary solution but need a solution that will work when i use it with a swtich statement soon as stated above and to understand what the problem is also.

Anyone know what else could be causing the problem?

Thanks
PHPLOVER

IE just sucks X_x

Hi,

I know it does but it's not a solution to say that. Many people still use IE and many people who visit my site use IE so i need a solution. Someone is bount to know what is wrong.

It's simply trying to figure out what is wrong with my script and not just bumping the browser off. I think it has something to do with the readfile and/or filesize.

Still looking for a solution people.

Thanks
PHPLOVER

From some further reading, it seems that application/x-zip-compressed is the mime type to use rather than application/zip. You might want to give that a try.

Hi,

That does not work either. Although i cannot think why that would cause a problem i don't know, i did read online thou application/octo-stream is used for zip files also but either way it should work with the application/zip.

My only problem left now is the filesize, for some reason browsers aren't able to detect the filesize so person won't know file size and time left.

Any suggestions on why that maybe?

Thanks
PHPLOVER

Since you are downloading, you are trying to get the file size of a file on the server. This should work just fine on any browser because it is happening on the server not locally. If you give it a valid file system address (relative, c:/... home/ etc) then you should get the size.

Hi,

I am testing on server. The filepath etc is correct as it downloads the file but it for some reason is giving incorrect file size to each browser.

When i looked at the headers using live headers and Fiddler it said the server was telling the header it was for example 1.10Mb yet it is 1.12Mb.

As you can see it seems to be giving the wrong file size and since going from header('Content-Type: application/zip'); to header('Content-Type: application/octet-stream'); this is when it has stopped giving the file size, but as you know using application/zip or the application/x-zip-compressed it causes the download to be corrupt in IE and causes it for some people to stop at 99%.

I looked online and these are the headers that can be used for .zip files:

.zip application/x-compressed
.zip application/x-zip-compressed
.zip application/zip
.zip multipart/x-zip

It seems two of them will show the file size but when they do show the filesize they then only download 1.11MB which causes the zip file to be corrupt as it is 1.12Mb. I have tested with many other zip files and the same thing. But the ones that don't show file size download the whole 1.12Mb without problems. :confused:


My code is like this:

<?php	
		include ("".$_SERVER['DOCUMENT_ROOT']."/includes/global.php"); // include global file
		require_once("".$_SERVER['DOCUMENT_ROOT']."/includes/session.php"); // include session
		memberonlyzoneerror(); // check if user is logged in
		
		if( isset( $_GET['filename'] ) ) {
			// get filename
			$getfile = $_GET['filename'];
			
			// path to file
			$filepath = "/home/path/to/file/downloads/files/$getfile";
			
			// check file exisits, if it does force download/header
			if ( file_exists( $filepath ) ) {
			
			// Headers
			header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
			header('Cache-Control: no-store, no-cache, must-revalidate');
			header('Cache-Control: post-check=0, pre-check=0', FALSE);
			header('Pragma: no-cache');
			header('Content-Type: application/octet-stream');
			header("Content-Disposition: attachment; filename=\"$getfile\"");
			header("Content-Transfer-Encoding: binary");
			header("Content-Length: ".filesize($filepath));
			readfile($filepath);
			
			} else { // else if file does not exist tell them and exit.
				@include ("".$_SERVER['DOCUMENT_ROOT']."/includes/top.inc");
				echo "<h2> File Does Not exist </h2>";
				echo "<p> The file you have requested does not exist. If you feel you received this message in error, please contact us.</p>";
				redirect ("index.php", "3");
				include ("".$_SERVER['DOCUMENT_ROOT']."/includes/footer.inc");
				exit;
			}
		} else {
			@include ("".$_SERVER['DOCUMENT_ROOT']."/includes/top.inc");
			echo "<h2> Missing an important parameter </h2>";
			echo "<p>You have used a false link, this page is missing an important parameter. Please report this to us.</p>";
			redirect ("index.php", "8");
			include ("".$_SERVER['DOCUMENT_ROOT']."/includes/footer.inc");
			exit;
		}
	


?>

Thanks
PHPLOVER

Just thought i would bump as still looking for a solution to why the file size is unknown using some of the zip mime types yet some others show the size but don't download the full file making it corrupt once downloaded.

Thanks
PHPLOVER

If application/octet-stream works, why not use it? The only problem you mentioned is that Opera reports the file as 1.2MB instead of 1.12MB. This is probably not an error on your part. Opera usually rounds up all sizes to the tenths place so a file might be 12.56 MB but Opera will show it as 12.6 MB for succinctness. As long as the file downloads properly and reports the correct size when you view it in Explorer (as in viewing files on your computer, not Internet Explorer), why bother?

Hi,

The reason why is because users don't know how large the file is as it says unknown in all browsers. Plus downloading from other websites are fine and shows the correct size etc so can't see what the problem is with it reading the file size of the file. using application/octet-stream is fine but is annoying about the file size not showing, also opera says an error when the file has downloaded in it's own download manager althou it opens up fine. This may confuse some people as they may think the download failed even thou it hasn't.

Thanks
PHPLOVER

I ran a little test program from my (virtual) linux server. I ran it from IE, Firefox, Opera and Chrome. It checks the filesize for another program in the same directory. It gave exactly the same (correct) result in every case. You must have a problem or limitation in your server environment or something wrong in the program or a problem in one or more browsers (try another version?). The program that I used was:

<?PHP
/*
     Test filesize (change the module name to check to something in the same directory)
*/

     $size = filesize ("div_by_zero.php");     
     echo "<br>div_by_zero.php reported as size=$size ";    
?>

If you run this and you don't get a value at all then something strange is happening on the server. If you don't get a value (or it is inconsistent) in different browsers then there could be a problem with the version of one or more browsers that you are using.

Hi,

I done a test and it works, it shows the file size on the webpage like it should. I am also using latest versions of all browsers.

For some reason all browsers just can't detect the filesize, but as i said before some zip mime types will detect filesize but then they don't download the full file making it corrupt, and the ones that don't detect file size will download it fully without corruption etc. This is what i find confusing.

Thanks
PHPLOVER

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.