从数据库下载 [英] downloading from database
问题描述
我有一个小问题的php内容处置,我有点了解问题在哪里,但我不知道如何解决它(新的使用数据库)。调用这个php页面将导致不显示任何回波,并只显示下载框,我打算只为cv(不确定是否这样工作,因为我收到的可下载文件无法打开) p>
删除标题(内容...
行将导致显示回声,但我不能
$ newEmployeeName = $ _POST。下载指定的文件,我希望它显示为一个链接, ['name'];
$ newEmployeeArea = $ _POST ['area'];
$ newEmployeeCV = $ _POST ['cv'];
include('databaseConnection.php ');
$ result = mysql_query(SELECT * FROM participants);
while($ row = mysql_fetch_array($ result))
{
$ download_me = $ row ['cv'];
header(Content-Disposition:attachment; filename = $ download_me);
echo $ row ['name']。 。$ row ['area_of_exp']。。$ download_me;
echo< br />;
}
解决方案
Content-Disposition
它作为下载后回显。通常,通过从文件系统中读取文件来使用它,因此您可以将其作为下载来提供。在你的情况下,如果你存储CVs在你的服务器上,那么你可以提供他们作为下载如下:?php
$ sql =SELECT * FROM table WHERE id =:id LIMIT 1;
$ stmt = $ db-> prepare($ sql);
$ stmt-> bindParam(':id',$ id,PDO :: PARAM_INT);
$ stmt-> execute();
$ row = $ stmt-> fetchObject();
if($ row){
header('Content-Disposition:attachment; filename ='。$ row ['filename']);
readfile($ uploads_dir。$ row ['filename']);
exit;
}
else {
die('CV请求无效。
}
显然,上面是一个简化版本的过程,你需要调整
此外,不要使用
mysql _
函数。它们已被弃用(根据此页上的警告)。使用 PDO 或新的 MySQLi (MySQL改进)扩展。I have a small problem with the php content-disposition, I kind of understand where the problem lies but I have no idea how to solve it (new to using databases). Calling this php page will result in not showing any of the echos and only showing the download box, which I intended for the "cv" only (not sure if it's working that way, because the downloadable file I receive cannot be opened)
Removing the
header(content...
line will result in showing the echos, but I won't be able to download the specified file. I want it to show as a link which would download its contents when clicked.$newEmployeeName = $_POST['name']; $newEmployeeArea = $_POST['area']; $newEmployeeCV = $_POST['cv']; include('databaseConnection.php'); $result = mysql_query("SELECT * FROM participants"); while($row = mysql_fetch_array($result)) { $download_me = $row['cv']; header("Content-Disposition: attachment; filename=$download_me"); echo $row['name'] . " " . $row['area_of_exp'] . " " . $download_me; echo "<br />"; }
解决方案The
Content-Disposition
header will force the script to present anything echoed after it as a download. You would normally use this with reading a file from the file system, so you can offer that as a download. In your case, if you’re storing CVs on your server then you may offer them as a download as follows:<?php $sql = "SELECT * FROM table WHERE id = :id LIMIT 1"; $stmt = $db->prepare($sql); $stmt->bindParam(':id', $id, PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchObject(); if ($row) { header('Content-Disposition: attachment; filename=' . $row['filename']); readfile($uploads_dir . $row['filename']); exit; } else { die('Invalid CV requested.'); }
Obviously the above is a simplified version of the process and you will need to tweak it to fit your application, but that’s the gist of it.
Also, don’t use the
mysql_
functions. They’re deprecated (as per the warning on this page). Use either PDO or the new MySQLi (MySQL improved) extension.这篇关于从数据库下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!