ruchi
ruchi

Reputation: 83

Unable to download csv using php script

I have searched existing questions on this topic but I couldn't find the solution. Requesting your attention to help me solve this problem.

I am trying to create a script to get csv file from a distant source (and upload it to the DB). I am getting the following error:

Warning:fopen(http://www.nseindia.com/products/content/sec_bhavdata_full.csv): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /home/path/csvUploadScript.php on line 20 Error opening data file.

I have checked php.ini file: allow_url_fopen is set "On"

This is the code I have to get the file contents:

`define('CSV_PATH','http://www.nseindia.com/products/content/');
$csvfile = CSV_PATH . "sec_bhavdata_full.csv"; 
$file = fopen($csvfile,"r");`

The uploading part works alright when the csv file is available on the same server but it gives error when I try to upload the csv directly from the nseindia site.

What am I doing wrong?

Upvotes: 2

Views: 1280

Answers (3)

Haseeb
Haseeb

Reputation: 2234

add this on top of your code before echo the output

    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=" . date('Y-m-d H.i.s') . ' - file-namw.csv');
    header("Pragma: no-cache");
    header("Expires: 0");

Upvotes: 1

user3111011
user3111011

Reputation: 145

Put these lines in your code

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="your filename.csv"');

Upvotes: 1

Nishant
Nishant

Reputation: 358

header("Content-Description: File Transfer"); 
header("Content-Type: application/octet-stream"); 
header('Content-Disposition: attachment; filename="your filename.csv"'');

Upvotes: 0

Related Questions