简体   繁体   中英

Download a file using php

I want to download a file from the webiste " http://nccpl.com.pk/market-information/fipi-lipi/fipi-sector-wise " but there is the extract button to download the excel file. How can I download it automatically on daily basis using php.

The data export link is generating the CSV in JavaScript in the browser from the table. For this reason, you will not be able to download it using just PHP, since the file does not actually exist anywhere.

You might, however, be able to generate the file from the table in nothing but PHP by downloading the html of the page and parsing it. However, I do not know tools are available for that purpose in PHP.

believing that the file gets updated automatically you can just use

header('Location: /path-to-file/filename');

Example

<?php
  header('Location: '/docs/pdf/mypdf.pdf');
?>

just write this into a .php file and let the user to the .php file when he clicks the download buttton. Make sure the file has permissions to be downloaded.

readfile($file)

Is what you need my friend. This is a snippet from php.net

<?php
$fichero = 'mono.gif';

if (file_exists($fichero)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($fichero).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($fichero));
    readfile($fichero);
    exit;
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM