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.
Asked
Active
Viewed 420 times
-10
-
1@Fred-ii- I would guess the OP does not only want us to write the php code to get the file, but also add some mysqli magic to add it to his database :-) – jeroen Feb 28 '17 at 13:42
-
1you must using Curl and cronnjobs – hasan movahed Feb 28 '17 at 13:44
3 Answers
1
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.

Mobius
- 2,871
- 1
- 19
- 29
-1
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.

Derek Wang
- 10,098
- 4
- 18
- 39

Kuldeep
- 489
- 6
- 10
-
if a user is involved, how is this downloading "automatically" as specified in the question? – ADyson Oct 11 '17 at 05:50
-
what you can do is create a button like this Download or add this into a button to download a file automatically using php – Kuldeep Oct 11 '17 at 10:00
-
Yes but that's still a process which the user has to invoke by clicking the button. OP wants to download "automatically on a daily basis", which really implies no user intervention. Making a user do it every day is unreliable and a long way from "automatic". It needs an automated script triggered by a timer (e.g. cron or Windows Scheduled Tasks), not a GUI. Just making a button which redirects a user to another link is not automation. – ADyson Oct 11 '17 at 10:16
-
well found this.... dig a little googling https://stackoverflow.com/a/15760383/6051682 hope this is what is needed... – Kuldeep Oct 11 '17 at 10:22
-
yes something based on that would make a much better answer. However, note Mobius' answer - in the specific case of the website mentioned by the OP this may not be technically possible at all, automated or not. – ADyson Oct 11 '17 at 10:24
-2
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;
}
?>

J.C.
- 144
- 1
- 5
-
1This answer is for getting a website to *serve* a download *that it already hosts*, which is not at all what the question is about, namely web scraping of dynamically-generated files. – Nathan Tuggy Aug 21 '17 at 03:45