Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello , this is my code to download files with php , but , when I download a file (for example Excel 2007 file) excel doesn't open it. Where is the problem

PHP
$document = urldecode($_GET['download']);
	$extension = end(explode('.',$document));
	$mimetype = '';
	
	switch($extension){
		case 'zip':
		$mimetype='application/zip';
		break;
		case 'rar':
		$mimetype='application/x-rar-compressed';
		break;
		case 'xls':
		$mimetype='application/vnd.ms-excel';
		break;
		case 'xlsx':
		$mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
		break;
		case 'doc':
		$mimetype='msword';
		break;
		case 'docx':
		$mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document';
		break;
		case 'ppt':
		$mimetype='application/vnd.ms-powerpoint';
		break;
		case 'pptx':
		$mimetype='application/vnd.openxmlformats-officedocument.presentationml.presentation';
		break;
	}
	header('Content-type:' . $mimetype);
	header("Content-Disposition:attachment;filename:download.". $extension);

  	readfile($document);
Posted
Comments
Sergey Alexandrovich Kryukov 7-Sep-12 14:38pm    
Can Excel open such file before it was up-loaded? How do you know the files stored on server are correct, of the compatible version, etc.?
--SA
Hysen Ndregjoni 7-Sep-12 15:29pm    
they worked before I uploaded , but I will check if there is a problem with the file's name
Thanks!
Hysen Ndregjoni
Sergey Alexandrovich Kryukov 7-Sep-12 16:24pm    
Well, can you download one of these files manually, using some available browser (if this is HTTP or HTTPS) or available FTP client (if the URL if FTP) and see if you can do it and the file is valid? This way, you can find out, if your client part code is responsible for a problem or not.
--SA
TRK3 7-Sep-12 16:54pm    
Upload a file, then download it. Then do a diff of the two files to see what is different.
Hysen Ndregjoni 7-Sep-12 16:23pm    
I checked it , but this happens again

Here is simple issue you should know.

even a single space would destroy the file. here is an example below:
example:
C++
<?php
//actually the line before <?php is a new line
file_dowloading_process();
?>


what is wrong in the above example? a new line at the beginning of the code. even if there is any new line or space is available it will destroy the file. so, remove all other useless echo or pure html lines and also if you are dealing with UTF-8 use UTF-8 file without BOM
 
Share this answer
 
v2
Comments
enhzflep 8-Sep-12 0:19am    
My 5 for explaining the problem with whitespace. Not the issue here, since doing so will prevent the modification of headers - which will show an error message as well as a dump of the file on screen in the browser, but an important point nonetheless.
Mohibur Rashid 8-Sep-12 1:15am    
thanks a lot :)
Hysen Ndregjoni 8-Sep-12 10:11am    
thanks
I've used this _very_ simple code. The file works just fine after being downloaded. I also note that your 2nd last line appears to be incorrect. You should have an equals [=] symbol after filename, not a colon[:]

PHP
<?php
	header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
	header('Content-Disposition: attachment; filename=download.xlsx');
	readfile('aria.xlsx');
?>



I note that it still functions when we comment-out the first line:
PHP
<?php
	// header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
	header('Content-Disposition: attachment; filename=download.xlsx');
	readfile('aria.xlsx');
?>


If you don't have it already, you should _really_ consider downloading the *enhanced* version of the php manual. It's the same as the regular version, albeit with the user contributions that you get on the website. It's about 6 or 7 meg of user examples and notes. It's light-years in front of the standard version.
You can get it here: Select mirror for php_enhanced_en.chm[^]
 
Share this answer
 
Comments
Hysen Ndregjoni 8-Sep-12 10:11am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900