使用浏览器的PHP存储在数据库中查看文件
我想查看的文件(即:Excel工作表/ PDF /图像)。存储在数据库中的浏览器
I'm trying to view files (i.e: excel sheets/pdf/images) on browser that are stored in database.
我已经写了一个code从数据库中下载的文件,它是工作,但我想在浏览器中显示。
I already wrote a code for downloading the files from the database and it is working but I want to display it in the browser.
下面是code:
<?php require_once('Connections/databasestudents.php'); ?>
<?php
$id = $_GET['id']; // ID of entry you wish to view. To use this enter "view.php?id=x" where x is the entry you wish to view.
$query = "SELECT fileContent, filetype FROM file where id = $id"; //Find the file, pull the filecontents and the filetype
$result = MYSQL_QUERY($query); // run the query
if($row=mysql_fetch_row($result)) // pull the first row of the result into an array(there will only be one)
{
$data = $row[0]; // First bit is the data
$type = $row[1]; // second is the filename
Header( "Content-type: $type"); // Send the header of the approptiate file type, if it's' a image you want it to show as one :)
print $data; // Send the data.
}
else // the id was invalid
{
echo "invalid id";
}
?>
什么情况是,view.php下载,并没有什么查看。
What happens is that view.php is downloaded and nothing is viewed.
有什么建议?
根据您的code, $行[1]
是文件名。内容类型头应包含的内容类型的替代,即文件的MIME类型,例如:
According to your code, $row[1]
is "the filename". The Content type header should contain the content type instead, i.e. the file mime type, for example:
header('Content-type: application/pdf');
如果您要添加的文件名:
If you want to add a filename:
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename='.$row[1]);
print $data;
务必 $数据
是文件的内容,有些东西你可以从的 ReadFile的()例如
Be sure $data
is the content of the file, something you can take from readfile() for example.
阅读手册上: http://php.net/manual/en/ function.readfile.php
请,虽然PDF和图片轻松浏览通过浏览器,我认为Excel中需要一些的特设的插件的。
Keep in mind that while PDF and images are easily viewable by a browser, I think Excel needs some ad hoc plugin for that.
一个更完整的示例的右出该手册的的,让你更彻底的想法(并非所有的标题是必要的,你应该相应地改变别人对你的code):
A more complete example right out of the manual, to get you a more thorough idea (not all those headers are necessary, and you should change others accordingly to your code):
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;