2013-08-16 26 views
0

我的问题是通过单击下载图标下载文件(任何扩展名),我尝试过使用打印语句,但它会打开一个新窗口并显示一个用户可以打印整个网页,但我需要让他们从查询中下载单个文件。 任何想法?从网页上的mysql结果下载文件

这是我的查询是如何显示,当图像在用户点击它应该能够下载

doc_id passport_no photo   nic_copy   passport_copy 
    200004 N8899999  1376630109.jpg 1376630110.jpg 1376630111.jpg 

,这是我的编码..

<?php if (isset($_GET['file'])) { 
$file = $_GET['file']; 
if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) { 
header('Content-Type: application/pdf'); 
header("Content-Disposition: attachment; filename=\"$file\""); 
readfile($file); 
} 
} else { 
header("HTTP/1.0 404 Not Found"); 
echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>"; 
} 
?> 

下载链接

<a href="http://localhost/visa/view.php?file=example.pdf">Click here to download PDF</a> 
+0

显示什么你试过 –

回答

0
<a href="view.php?file=<?php echo $row['filename']; ?>">Click here to download PDF</a> 
this will be your view.php 
<?php 
$f="upload/".$_REQUEST['file']; // upload is your destination folder where you uploading you files 
if(file_exists($f)) 
{ 
header('Content-Description: File Transfer'); 
header('Content-Type: application/pdf'); 
header('Content-Disposition: attachment; filename='.basename($f)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($f)); 
ob_clean(); 
flush(); 
readfile($f); 
exit; 
?> 
+0

感谢@chirag颂歌帮我解决我的问题,我只是做了我的代码 – YathuGulan

+0

变化最欢迎亲爱的:-) –

0

使用PH P标题告诉浏览器保存文件而不是显示它。

header('Content-disposition: attachment; filename=whatever.ext'); 
header('Content-type: mime/type'); 
//echo file contents out here