2014-01-27 102 views
1

我有我的MySQL数据库上的文件,我想创建一个链接(为我的变量$文件)下载这些文件。链接变量与PHP下载我的MySQL数据库上的链接

$reponse = $bdd->query("SELECT name, filename, filepath FROM documents"); 
while ($donnees = $reponse->fetch()) 
{ 
    $files=$donnees['filename']; 

    // [Doing some work here...] 

    if (preg_match($word,$line)) { 
     echo "<center>Word ".$word." found in the file ".$files."</center>";  
    } 
} 
+1

我知道法国人p人们对他们的语言感到非常自豪,但如果您要求帮助,请拜托,真的请...使用英语,特别是在代码中。 –

+0

我不以我的语言为荣:P我编辑了我的代码,对于这个错误感到抱歉。 – user3239930

回答

0

如果你的文件路径是本地路径(如/var/www/uploads/myfile.txt),你需要做一个新的PHP页面,允许用户下载文件。因此,有两个步骤:

  1. 写的下载网址在当前的环
  2. 下载创建一个新的页面

你的循环修改:

$reponse = $bdd->query("SELECT name, filename, filepath FROM documents"); 
while ($donnees = $reponse->fetch()) 
{ 
$files=$donnees['filename']; 
// ... stuff here ... 
if (preg_match($word,$line)) { 

    echo "<center>Word ".$word." found in the file <a href=\"download.php?file=".$filepath."\">".$files."</a></center>"; 

} 

在的download.php :

<?php 
$file=$_GET['file']; 

if (($file != "") && (file_exists("./" . basename($file)))) // add some checks to avoid the download of critical files... 
{ 
    $size = filesize("./" . basename($file)); 
    header("Content-Type: application/force-download; name=\"" . basename($file) . "\""); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: $size"); 
    header("Content-Disposition: attachment; filename=\"" . basename($file) . "\""); 
    readfile("./" . basename($file)); 
    exit(); 
} 
?> 
+0

谢谢你的帮助,它的工作! – user3239930