0

I am trying to have two files auto downloaded when an url is clicked. I have found the solution in this post How to force file download with PHP and using this code <?php header("Location: http://example.com/go.exe"); ?>

My goal is to have 2 files downloaded by extension type rather than having full filename url because filenames would be changed every 1-2 days but extensions would always remain same. The file extensions are .xls and .pdf. I looked into this post - How to force download different type of extension file php but didnt see the actual code I am looking for. Any guidance is appreciated. Thank you.

whatsup
  • 29
  • 1
  • 9
  • What exactly do you mean by "download by extension type". Could you provide an example? – Eriks Klotins May 21 '20 at 04:16
  • @EriksKlotins I have two files in that folder. one is filename.xls and other is filename2.pdf. The download needs to happen based on the extension .xls and .pdf and not the actual filename because names will be changed very often. – whatsup May 21 '20 at 04:23
  • You have to use PHP to scan the folder, locate the file, then return it with the right headers: https://stackoverflow.com/questions/8541180/best-way-to-get-files-from-a-dir-filtered-by-certain-extension-in-php – Eriks Klotins May 21 '20 at 05:10
  • can you send me download file url – Paras Raiyani May 21 '20 at 05:11
  • @EriksKlotins great !! that post has glob function which can be used. what would be the code after that to execute download ? – whatsup May 21 '20 at 05:35

2 Answers2

1

you need to solve two problems:

  1. locate the right file. You can do that in different ways depending on how you want it. See this link: Best way to get files from a dir filtered by certain extension in php

    // Returns one file from a folder with a specific extension
    // order is not guaranteed
    // locate_the_file_by_extension("/my/secret/folder/", "pdf")
    // folder/file must be readable by php
    function locate_the_file_by_extension($folder, $extension)
    {
       $files = glob($folder."*".$extension);
       if (count($files)>0)
       {
           return $files[0];
       }
       else
       { 
           throw new Exception("no files found");
       }
    
    }
    
  2. Download the file:

    // locate_the_file_by_extension('pdf') returns the file you want to download
    $filename = locate_the_file_by_extension();
    
    $size   = filesize($filename);
    
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Transfer-Encoding: binary');
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . $size);
    
    // read the file
    echo file_get_contents($filename);
    

Make sure nothing else is sent to the output. An extra output will corrupt the file or the headers.

Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26
  • Great. thank you for your time on this. I would like to use this function as per your suggestion to get the right extension "$files = array(); foreach (glob("/path/to/folder/*.txt") as $file) { $files[] = $file; }" , how to combine this code with yours please ? – whatsup May 21 '20 at 15:58
0

Hope so That will help you

$url="https://example.com/new.jpg";
$download_name = "new-image.jpg";
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . $download_name . "\"");
readfile($url);
Paras Raiyani
  • 748
  • 5
  • 10
  • Thank you. This code is not working when the filename is changed. – whatsup May 21 '20 at 04:31
  • @hawk, you need to adjust to your requirements. Give us more details if you still struggle with problem. – Jsowa May 21 '20 at 05:26
  • @Tajniak I meant the filename will not be same everyday. "new" will be changed to something different next day meanwhile .jpg extension would remain same. I cant call static filename. – whatsup May 21 '20 at 05:45
  • Provide proper filename everyday. Make some script, pull it from database etc. This is still PHP file. – Jsowa May 21 '20 at 05:47
  • can you put here some example file download url please – Paras Raiyani May 21 '20 at 05:50