Showing posts with label PHP Download File. Show all posts
Showing posts with label PHP Download File. Show all posts

Friday, 2 August 2019

How to force a CSV file to download when clicking on a download link using PHP

Example: We have a CSV file called example.csv we want users to download to use as a template. Now some browsers will display the contents of the file instead of asking users to download the file. What we want to do is to force the downloadinstead of displaying the contents in the browser.
  • Create a new PHP file called ‘download.php‘
  • Open the file for editing
  • Copy the code below into the ‘download.php‘ file
    {codecitation style=”brush: PHP;”}
    header(‘Content-Type: application/csv’); //Outputting the file as a csv file
    header(‘Content-Disposition: attachment; filename=example.csv’); //Defining the name of the file and suggesting the browser to offer a ‘Save to disk ‘ option
    header(‘Pragma: no-cache’);
    echo readfile(‘example.csv’); //Reading the contents of the file
    {/codecitation}
  • Now on the page where you want the user to click on the link to download the file add the following code
    {codecitation style=”brush: HTML;”}
    <a href=”/download.php”>Click here to download an example of the “CSV” file</a>
    {/codecitation}
  • Now everytime the user clicks on the link the browser won’t display the contents but will offer to download the file

Friday, 5 October 2018

PHP - FORCEFUL DOWNLOADING OF FILE

If you want certain files to download instead of opening in new tab, you can use the below snippet.
Syntax

Wednesday, 26 September 2018

PHP script for downloading files

As we see on many websites, there are file download links. When we click on the link, browser starts downloading. The question arises that can we do this ? of course, let's learn how to download a file with php. Suppose we have a pdf file which we want to allow a user to download. Here is simple coding for downloading a file with php. Downloading of file is done by sending different headers to browser and readfile function. In headers, we send information about file like type of file, file size, name etc. Make file download.php and write given coding in it.


<?php
//download.php
$filename="sample.pdf";
$file="c:\\myfile\\$filename";
$len = filesize($file); // Calculate File Size
ob_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public"); 
header("Content-Description: File Transfer");
header("Content-Type:application/pdf"); // Send type of file
$header="Content-Disposition: attachment; filename=$filename;"; // Send File Name
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len); // Send File Size
@readfile($file);
exit;

?>
Basic Php script for downloading a file coding is completed, our next step is providing download link in page like <a href="download.php">Download File</a>. So this was demo for pdf file download through php. In next step we are going to learn that how can we make common php script to download different kind of files? We are going to make common php function for download different kind of files.

<?php
   // function.php
 function download_file($filepath)
 {
  if (!is_file($filepath)) 
     {
    echo("404 File not found!"); // file not found to download
    exit();
     }
  
     
   $len = filesize($filepath); // get size of file
   $filename = basename($filepath); // get name of file only
   $file_extension = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
   //Set the Content-Type to the appropriate setting for the file
   switch( $file_extension )
     {
     case "pdf": $ctype="application/pdf"; break;
     case "exe": $ctype="application/octet-stream"; break;
     case "zip": $ctype="application/zip"; break;
     case "doc": $ctype="application/msword"; break;
     case "xls": $ctype="application/vnd.ms-excel"; break;
     case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
     case "gif": $ctype="image/gif"; break;
     case "png": $ctype="image/png"; break;
     case "jpeg":
     case "jpg": $ctype="image/jpg"; break; 
     default: $ctype="application/force-download";
    }
    ob_clean();
    //Begin writing headers
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public"); 
    header("Content-Description: File Transfer");
    //Use the switch-generated Content-Type
    header("Content-Type: $ctype");
    //Force the download
    $header="Content-Disposition: attachment; filename=".$filename.";";
    header($header );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$len);
    @readfile($filepath);
    exit;
  }
Now you can use above function to download pdf,jpg,doc and excel files etc. you need to give full path of the file rest function will take care of all. To use above function make a php file with name download.php and include above file function.php in it. And give full path of file which you want to download as parameter to download_file() function like given below.
 <?php
// download.php
 $filepath = "c:\\myfile\\test.doc";
 download_file($filepath);
In last step provide download link in page like <a href="download.php">Download File</a> where you want.

Monday, 24 September 2018

Download a file using curl in PHP

Here is a quick curl snippet for php, that can download a remote file and save it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
 
set_time_limit(0);
 
//File to save the contents to
$fp = fopen ('files2.tar', 'w+');
 
 
//Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20",$url));
 
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
 
//give curl the file pointer so that it can write to it
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 
$data = curl_exec($ch);//get curl response
 
//done
curl_close($ch);
The CURLOPT_FILE option takes a file resources and writes the content of the url to that file resource/handle. Also set the script time limit to something large so that the script does not end when downloading large files.