如何使用php脚本下载特定文件(csv)


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

im从php脚本生成每日报告,它位于如下所示的文件夹中

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

我向用户发送了一封带有下载链接的电子邮件,一旦他们点击下载链接,下载脚本就会触发并准备下载。它目前将所有文件放入一个数组中,对其进行排序,并找到最新的下载文件

这种方法有一个流程,即使你转到一封2周前的电子邮件并点击下载链接,它也会给你最新的报告,而不是两周前的报告。

那么有人能告诉我一种方法,在我的电子邮件中发送下载链接,并与相应的文件建立关系吗

电子邮件脚本

$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";
    }

下载脚本

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

您可以在下载链接上使用指示报告日期的查询参数/CSCDownload.php?日期=2015/02/17

这将允许您从可用列表中选择相应的报告。

您可以使用类似的东西作为下载脚本。我只做了一些微小的改变就可以让它发挥作用:

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

现在,您可以在浏览器中使用"example.com/getcsvfile.php?date=2015_02_15"调用脚本,而不用担心注入,因为您可以检查该文件是否是csv文件之一。