0

I want to build a website which mainly shows data in charts. Data for charts should come from MySQL database. Currently I don't know how to save a CSV file on my server to feed my database later.

The problem is that the file is saved on another server and only offered by hyperlink which references to an PHP file (f.e. www.asdf.com/fore_csv_download.php).

Is there a possibility to download the CSV file to my Server?

Once the File is saved I should be able to load data into the database with this: MYSQL automatically insert csv files from folder using mysqlimport

Later I'm thinking of automate this process with cron jobs.

2
  • Welcome to Stackoverflow. I'm currently reviewing your question and it's quiet unclear to me at the moment. Could you please elaborate more on your setup? I barely read there are two servers and you want to download a file from one server to the other and then read that file into a database. May 3, 2015 at 7:20
  • php.net/manual/en/function.file-get-contents.php - You can try to use this function to get file from remote server and then save it on your disk May 3, 2015 at 7:46

1 Answer 1

1

If need retrieve file in other server use CURL, eg:

<?ph
$source = 'http://www.asdf.com/fore_csv_download.php';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);

file_put_contents('/var/www/data/file.csv', $data);

If need download for local, create a page like this (eg. download.php):

<?php
$file = FULL PATH CSV FILE from "Database" (only a example);

header('Content-Type: text/csv');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="' . basename($file) . '"');

echo file_get_contents($file);
1
  • Thanks! Your first example is what I was looking for. Works now!
    – anho
    May 3, 2015 at 9:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.