php – How to download files automatic or itself (images, zip, pdf and other files)

php – How to download files automatic or itself (images, zip, pdf and other files)

In this post we are going to show you, how to download files in php from web server to local machine. Download files is just a simple and easy process. we do not need more lines or complicated code to Download files automatic or itself.

For make easy code works with header to download and read a file. we use below code to download any of files like images, zip, jpg, png, pdf, csv etc. Default most of the file types (csv, pdf, gif, png, html, jpg, txt etc.) displayed in browser instead of download.

But using code we can force to browser for download these files rather showing them. In this code we are using just headers of php to force download and setting up content types. following code will help you to download files automatic or itself.

function file_download($file_name){
    if(!empty($file_name)){
	
		// add your file path.
		// like '/uplods_file/'
		$download_path = ''; 
		$download_file =  $download_path.$file_name;
		
		// Check file is exists on given path.
		if(file_exists($download_file))
		{
			// Get download file extension.
			$file_extension = explode('.',$file_name);	
			// get file extension of download file 	
			$file_extension = $file_extension[count($file_extension)-1];
			
			// set header for-
			// For Gecko browsers
			header('Content-Transfer-Encoding: binary');  
			header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($download_path)) . ' GMT');
			
			// set header for-
			// Supports for download file resume
			header('Accept-Ranges: bytes');
			
			// set header for-
			// Calculate download file size
			header('Content-Length: ' . filesize($download_file));
			// set header for Content Encoding		
			header('Content-Encoding: none');
			
			// set header for-
			// Change the mime type if the file is not PDF
			header('Content-Type: application/'.$file_extension);  
			
			// set header for-
			// Make the browser display the Save As dialog
			header('Content-Disposition: attachment; filename=' . $file_name);  
			
			// read download file
			readfile($download_file);
			
			// exit to function
			exit;
		}
		else
		{
			// file does not exists
		    echo "File does't exists on given path!";
		} 
    }
	// exit to function
	exit;
}

Calling function “file_download” :- Add above function to coding file and just call the download function “file_download” and pass file name.

Example ::

// call the function "file_download"
// we can pass any types of file
// like ex .pdf, .png, .jpg, .mp4 etc. 
file_download('your-file-name.pdf');

By using this script or code we can beel to download all types of files. Just put the function “file_download” in code file and pass filename and set your path of file, rest of work leave for this function and it will download files automatic or itself.

Leave a Comment

Your email address will not be published. Required fields are marked *

2  +  8  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US