简体   繁体   中英

How to download a specific file(csv) using a php script

im generating a daily report from a php script, it is residing in a folder as follows

report_2015_02_15.csv
report_2015_02_16.csv
report_2015_02_17.csv
report_2015_02_18.csv
report_2015_02_19.csv

And im sending my users an email with link to download, once they click the download link the download script triggers and prepares the download. It currently gets all the files into an array sorts it and finds the latest for the download,

this method has a flow in it where, even when you go to a email that is 2 weeks older and clicks the download link, it gives you the latest report instead of giving the two weeks older report.

so can anybody tell me a way to send the download link in my email with a relationship to its corresponding file?

email script

$down_link = Config_Reader::readProjectConfig('tad')->base_url.'CSVDownload.php;

$mail = new Zend_Mail ();
$sentFromEmail = $config->sentFromrec;
$tr = new Zend_Mail_Transport_Sendmail ( '-f' . $sentFromEmail );
Zend_Mail::setDefaultTransport ( $tr );
$mail->setReturnPath($sentFromEmail);
$mail->setFrom ( $sentFromEmail, 'Reporting' );

$email_body = "Hi,<br /><br />Download the weekly details of adtracker projects, by clicking the link below.
<br /> <a href=".$down_link.">Report</a><br /><br />Thank You.";

$mail->setBodyHtml ( $email_body );
$mail->addTo ( $weeklyReportRec);
$mail->setSubject ( "Report" );

try {
        $mail->send ();
    } catch ( Exception $e ) {
        echo "Mail sending failed.\n";
    }

download script

$basePath = "$download/dailyReport/";
$csvFiles = glob($basePath."/*.csv");
if(count($csvFiles)){
    array_multisort($csvFiles, SORT_DESC);
    $csvFile = $csvFiles[0];
}else{
    exit("Server error!");
}

$h = fopen($csvFile, "r");
$contents = fread($h, filesize($csvFile));

You can use a query parameter indicating the report date on the download link. .../CSCDownload.php?date=2015/02/17

This will allow you to select the respective report from the available list.

You can use somthing like that as download script. I made just minum changes to get it work:

$date = $_GET['date'];
$basePath = "$download/dailyReport/";
$csvFiles = glob($basePath."/*.csv");

$file = $basePath . 'report_' . $date . '.csv';
$found = false;

foreach($csvFiles as $csvFile){
    if($file === $csvFile){
        $found = $csvFile;
        // You have found the file, so you can stop searching
        break;
    }
}
if(false === $found){
    exit("Server error!");
}

$h = fopen($found, "r");
$contents = fread($h, filesize($found));

Now you can call you script in your browser with "example.com/getcsvfile.php?date=2015_02_15" without worrying about injection, because you check if the file is one of the csv files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM