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.

See also  PHP Mailgun Sending Emails Tutorials - Programming

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){
		// add your file path.
		// like '/uplods_file/'
		$download_path = ''; 
		$download_file =  $download_path.$file_name;
		// Check file is exists on given path.
			// 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
			// exit to function
			// file does not exists
		    echo "File does't exists on given path!";
	// exit to function

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

See also  How to get a current year in Python

Example ::

// call the function "file_download"
// we can pass any types of file
// like ex .pdf, .png, .jpg, .mp4 etc. 

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 *

72  +    =  78

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