2

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?

3 Answers 3

1

Put these lines in your code

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="your filename.csv"');
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for reply. Running the script with these lines, downloads the file to my PC. I want to fetch content of the csv and upload it in DB directly without downloading. I did not share the uploading part of script above but the same script is supposed to use the csv for data upload to DB as well.
1

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");

2 Comments

thanks for reply. Adding these lines, downloads the file to my PC. I want to fetch content of the csv and upload it in DB directly without downloading.
@ruchi dear you can find simple solution here codechewing.com/library/… and if this solution is work for you please accept the answer
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.