簡體   English   中英

如何使用php腳本下載特定文件(csv)

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

即時消息從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

我會向我的用戶發送一封包含下載鏈接的電子郵件,一旦他們單擊下載鏈接,下載腳本就會觸發並准備下載。 當前,它將所有文件放入一個數組中進行排序,並找到最新的下載文件,

此方法具有一定的流程性,即使您轉至兩周前的電子郵件並單擊下載鏈接,它也會為您提供最新報告,而不是兩周前的報告。

所以有人可以告訴我一種方法來發送電子郵件中與其相應文件相關的下載鏈接嗎?

電子郵件腳本

$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?date = 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文件之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM