Grab a File & Put It On My Server

Hi,
I’m using the following code to grab a csv file from one website & keep it on my website. The code is working fine.

<?php 
$url  = 'http://thewebsite.com/export.aspx?type=csv&report=nameofthereport~8~';
    $path = '/home/path/to/mywebsite.com/public_ftp/incoming/nameofthereport_Area_8.csv';
    $fp = fopen($path, 'w');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);

    $data = curl_exec($ch);

    curl_close($ch);
    fclose($fp);
?>

Here’s what I want to do next. I want to query a database, and replace nameofthereport and 8 with the results of the query. So the main idea is to grab multiple csv files of different names from the same website.

The difference in the file names are reflected in the $report and $district variable (referring to the code snippet below). For example, I want to grab the following files:-
http://thewebsite.com/export.aspx?type=csv&report=salesreport~1~
http://thewebsite.com/export.aspx?type=csv&report=employeereport~2~
http://thewebsite.com/export.aspx?type=csv&report=productionreport~3~

From the above example, salesreport, employeereport and productionreport are stored in the report column of the database. 1,2,3 are stored in the district column of the database.

My code to query the database is below.

<?php 
include("/home/path/to/mywebsite.com/public_html/db_connect.php"); //connect to the database
$today = date('Y-m-d');
$query1=("SELECT * FROM `table_name` WHERE expiry > $today");
$result1=mysqli_query($GLOBALS["___mysqli_ston"], $query1);
$num1=mysqli_num_rows($result1); 

if ($num1 > 0) { 
while ($data = mysqli_fetch_array($result1)){ 
$email_to = $data["email"]; 
$district = $data["district"];
$report = $data["report"];
                      }
                                                                      }
?>

Based on the code above, I would like to replace nameofthereport with $report and 8 with $district

Could you show me how to put the first code snippet ‘into’ the second code snippet so that it loops properly?

Thanks.

I’d probably wrap your download routine as a function, then do something like this (which I haven’t tested, I should say):

<?php 
include("/home/path/to/mywebsite.com/public_html/db_connect.php"); //connect to the database
$today = date('Y-m-d');
$query1=("SELECT * FROM `table_name` WHERE expiry > $today");
$result1=mysqli_query($GLOBALS["___mysqli_ston"], $query1);
$num1=mysqli_num_rows($result1); 

if ($num1 > 0) { 
while ($data = mysqli_fetch_array($result1)){ 
  $email_to = $data["email"]; 
  $district = $data["district"];
  $report = $data["report"];
  getfile($district, $report);
  }
}

function getfile($dist, $rept) {
    $url  = 'http://thewebsite.com/export.aspx?type=csv&report=' . $dist . '~' . $rept . '~';
    $path = '/home/path/to/mywebsite.com/public_ftp/incoming/' . $dist . '_Area_' . $rept . '.csv';
    $fp = fopen($path, 'w');
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    $data = curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    }

?>

You’d probably want to add some error checking to return true or false to indicate whether the file downloaded or not, or maybe it should just run silently.

1 Like

Wonderful help, droopsnoot. Thanks a lot. I appreciate it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.