如果我转到http://site.com/uploads/file.pdf我可以检索文件。
但是,如果我有一个脚本,如:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
//require global definitions
require_once("includes/globals.php");
//validate the user before continuing
isValidUser();
$subTitle = "Attachment";
$attachmentPath = "/var/www/html/DEVELOPMENT/serviceNow/selfService/uploads/";
if(isset($_GET['id']) and !empty($_GET['id'])){
//first lookup attachment meta information
$a = new Attachment();
$attachment = $a->get($_GET['id']);
//filename will be original file name with user name.n prepended
$fileName = $attachmentPath.$_SESSION['nameN'].'-'.$attachment->file_name;
//instantiate new attachmentDownload and query for attachment chunks
$a = new AttachmentDownload();
$chunks= $a->getRecords(array('sys_attachment'=>$_GET['id'], '__order_by'=>'position'));
$fh = fopen($fileName.'.gz','w');
// read and base64 encode file contents
foreach($chunks as $chunk){
fwrite($fh, base64_decode($chunk->data));
}
fclose($fh);
//open up filename for writing
$fh = fopen($fileName,'w');
//open up filename.gz for extraction
$zd = gzopen($fileName.'.gz', "r");
//iterate over file and write contents
while (!feof($zd)) {
fwrite($fh, gzread($zd, 60*57));
}
fclose($fh);
gzclose($zd);
unlink($fileName.'.gz');
$info = pathinfo($fileName);
header('Content-Description: File Transfer');
header('Content-Type: '.Mimetypes::get($info['extension']));
header('Content-Disposition: attachment; filename=' . basename($fileName));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($fileName));
ob_clean();
flush();
readfile($fileName);
exit();
}else{
header("location: ".$links['status']."?".urlencode("item=incident&action=view&status=-1&place=".$links['home']));
}
?>
这导致向我发送文件,但是当我打开它时,我收到一条错误说:
"File type plain text document (text/plain) is not supported"
答案 0 :(得分:1)
首先,我首先检查HTTP标头。您可以使用“Live HTTP headers”扩展轻松地在Firefox中执行此操作;不确定其他浏览器中的等价物。这将让您验证标头是否实际设置为“application / pdf”以及您的其他标头是否也已设置。
如果没有设置任何标头,您可能会在调用header()
之前无意中发送输出。 <?php
标记之前是否有空格?
答案 1 :(得分:0)
你确定application / pdf是浏览器实际看到的标题吗?
您可以使用各种HTTP开发工具进行检查,例如适用于Mac的HTTP Client或适用于Firefox的Firebug。
答案 2 :(得分:0)
我使用这个并且它有效。
if(file_exists($file_serverfullpath))
{
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
//sending download file
header("Content-Type: application/octet-stream"); //application/octet-stream is more generic it works because in now days browsers are able to detect file anyway
header("Content-Disposition: attachment; filename=\"" . basename($file_serverfullpath) . "\""); //ok
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($file_serverfullpath)); //ok
readfile($file_serverfullpath);
}
答案 3 :(得分:0)
尝试预先添加“error_reporting(0);”。我在http://php.net/readfile的评论中找到了这一点(你从这里得到了这个例子)。
另一个可能是问题的是文件大小。过去有一些关于PHP5的问题(我们在这里谈论2005年,所以我希望现在已经解决了这个问题)在阅读文件&gt; 2MB时遇到问题。如果您的文件大小超过此值,您可能需要验证它是否读取整个文件。