0

I am trying to download a remote pdf file in codeigniter which is password protected. Actually i have the username and password for the file, but i need to download the file directly without prompting by asking username and password for viewing it.I was trying with the below code in controller, but i am not sure that i tried correctly or not.

Actually, downloading is happening, but after opening the file its showing 'Failed to load PDF document'.

One more thing, i dont have a pre idea about the filename of the file to be downloaded, one file should be there corresponding to a cart id. So i am not sure whether the line header("Content-disposition: attachment; filename=tickets.pdf"); i used in below code is correct or not. I supposed it must be our custom file name for the file to be downloaded.

Below is the code in controller.

function file_download(){
    $cart_id = $this->uri->segment(3);//Getting cart id from the url
    $ticket_url=Base_url.'carts/'.$cart_id.'/tickets';

    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=tickets.pdf");
    ob_clean(); flush(); 
    readfile($ticket_url);
    exit();

}

Thanks in advance!

anees ma kader
  • 87
  • 1
  • 1
  • 10
  • Hopes this helps https://stackoverflow.com/a/364950/11926970 and https://www.tutorialrepublic.com/php-tutorial/php-file-download.php – Not A Bot Dec 24 '19 at 12:40
  • Hey Thanks, i tried both, downloading happening, but not a valid file(unknown format). Also i dont have a pre idea about the file name. I only have a url that points to the file and even the url does not contain the file name. – anees ma kader Dec 24 '19 at 13:01
  • Hey @annes Ma kader you will be needing the name of the file which you wanted to download. If for security you don't want the user to know the actual name of the pdf file, you can change it, but you need the name of the file. – Not A Bot Dec 26 '19 at 07:16
  • Hi, thanks!. Is there any way in php to get the file name if the file name is not included in the url also there is no idea about the name of the file which belongs to that url? – anees ma kader Dec 26 '19 at 07:55
  • Are you generating the **PDF** of from your own code, or something else? 1) There has to be any reference like a pointer or something that points to the file stored on the server, suppose **User A** has booked a flight ticket, and now you wanted to make that ticket inform of the `PDF` download automatically(assuming everything including payment was done completely), so now somewhere in the database (assuming using MySQL) you have stored the data like `order_id` `user_name`, etc. and the `PDF` is automatically generated on the server is subdomain like www.example.com/download – Not A Bot Dec 26 '19 at 10:59
  • 2) Nowhere when you are generating the `PDF` generate linking with the user may be user id is **user-0001** so generate a file like www.example.com/download/ticket-user-0001.php Now you can figure out the file name of the `PDF` – Not A Bot Dec 26 '19 at 11:01
  • Actually the url is a third party api url(i missed this to inform), thats why its asking username and password while loading the pdf. I am passing the unique parameter (say cart id) through the url. Then using the cart id, they fetch the data related to that cart id and generate the ticket(pdf file). Now i am checking whether any way to download that file without prompting by asking api credentials? – anees ma kader Dec 26 '19 at 11:12
  • 1) Okay, so you wanted your personal credentials should be not be asked when the user is downloading the `PDF`. See I advise you to use **cURL** in `PHP`. Using that you can download the pdf without the need of credentials to be added on the download(As credentials will be added during making the API request using **cURL**). – Not A Bot Dec 26 '19 at 11:24
  • 2) Another way is by using the `.env` the file where you save your credentials and pass the file when making an API call. I suppose you are using `file_get_contents()` for the API call. Try using [cURL](https://www.geeksforgeeks.org/php-curl/) For more clear reference [cURL](https://docs.msg91.com/collection/msg91-api-integration/5/sendotp/TZ648D1Y) click on sample code and then click on **PHP** then you will clear vision of using `cURL` in **PHP** – Not A Bot Dec 26 '19 at 11:25

0 Answers0