I want to download "Bhavcopy.cvs" file form "bseindia.com" on everyday at 5 pm. automatically.
So, I want php code that automatic download this file from "nse" website...
Please help.. The URL goes as follows
http://www.bseindia.com/download/BhavCopy/Equity/EQ140316_CSV.ZIP
I want to build an automated function to automatically upload reports from a URL at end of the day if found on that URL. My question is the URL keeps changing daily with a date.
I want to download this in csv and save it to my server automatically..A quick help will be really appreciated. Thanks in advance

Dani AI

Generated

A concise, practical approach that complements the thread: confirm the exact daily filename pattern (as observed) and the market timezone, then run a small CLI PHP script from the system scheduler that (1) checks the URL exists, (2) streams the ZIP to a temporary file, (3) atomically renames when complete, (4) optionally extracts the CSV, and (5) logs results and rotates old files. Keep the scheduler time aligned with the exchange publication time (server timezone vs. market timezone).

Example cron entry (server time; convert if needed):

0 17 * * * /usr/bin/php /srv/scripts/download_bhavcopy.php >> /var/log/bhavcopy.log 2>&1

Minimal, robust download pattern using cURL (script expects $url already built; write to .tmp then rename):

$tmp = '/srv/downloads/bhavcopy.tmp';
$out = fopen($tmp, 'w');
$ch  = curl_init($url);
curl_setopt_array($ch, [
  CURLOPT_FILE => $out,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_FAILONERROR => true,
  CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; bhavcopy-downloader/1.0)',
  CURLOPT_TIMEOUT => 300,
]);
$ok = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
fclose($out);

if ($ok) {
  rename($tmp, '/srv/downloads/bhavcopy.zip');   // atomic swap
  $zip = new ZipArchive;
  if ($zip->open('/srv/downloads/bhavcopy.zip') === true) {
    $zip->extractTo('/srv/downloads/');
    $zip->close();
  }
} else {
  unlink($tmp);
  error_log("Download failed: $err");
}

Troubleshooting notes and hard-won tips: run the script manually first; use absolute paths in cron; ensure file and directory permissions and PHP CLI binary path are correct; add a small retry/backoff for transient network errors; check HTTP response (HEAD) before download to skip market holidays; write logs and rotate them; and avoid exposing partial files by always downloading to a temp name then renaming. Mentioning again: first verify the site's filename/date format and publication time before scheduling.

I assume the last part of the URL, the file name, is just the date with 'EQ' in front?
If not, especially if there is no logic to file name, you'll have trouble.

Otherwise you can get the file now with:
$file = "EQ" . Date('dmy') . "_CSV.zip";
$url = "";

To save the file:
file_put_contents($file, fopen($url, 'r'));

Or you can use fopen to open the file and write it to folder using fwrite.

The file on the server has to exist of course and, depending on the size, you have memory issues and need to look at

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.