通过PHP服务文件的用户通过HTTP [英] Serve file to user over http via php
问题描述
如果我转到 http://site.com/uploads/file.pdf 我可以检索文件。
不过,如果我有一个脚本,例如:
< PHP的ini_set('的display_errors',1);
使用error_reporting(E_ALL | E_STRICT);//需要全球性的定义
require_once(包括/ globals.php);
//继续之前验证用户
isValidUser();
$字幕=附件;
$ attachmentPath =在/ var / www / html等/开发/的ServiceNow /自助服务/上传/;
如果(使用isset($ _ GET ['身份证'])和!空($ _ GET ['身份证'])){
//首先查找附件元信息
$ A =新的附件();
$附件= $ A->获得($ _ GET ['身份证']);
//文件名,将与用户原始文件名name.n prepended
$文件名= $ attachmentPath $ _ SESSION ['nameN']。' - '$ attachment-方式>。FILE_NAME;
//实例化连接块新attachmentDownload和查询
$ A =新AttachmentDownload();
$块= $ A-> getRecords(阵列('sys_attachment'=> $ _ GET ['身份证'],'__order_by'=>'位置'));
$跳频=的fopen($文件名'广州','W');
//读取的base64 EN code文件内容
的foreach($块为$块){
FWRITE($ FH,base64_de code($ chunk->数据));
}
FCLOSE($ FH); //打开文件名写作
$跳频=的fopen($文件名,'W');
//打开filename.gz提取
$ ZD = gzopen($文件名,R,广州');
//迭代文件,并写入内容
而(!的feof($ ZD)){
FWRITE($ FH,gzread($ ZD,60 * 57));
}
FCLOSE($ FH);
gzclose($ ZD);
取消链接($文件名。'广州');
$信息= PATHINFO($文件名); 标题(内容说明:文件传输');
标题(内容类型:.Mimetypes ::得到($信息['扩展']));
标题(内容处置:附件;文件名='基本名($文件名));
标题(内容传输编码:二进制');
头('过期:0');
标题(缓存控制:必重新验证,检查后= 0,pre-检查= 0');
头('杂注:公开');
标题(内容长度:文件大小($文件名));
ob_clean();
冲洗();
ReadFile的($文件名);
出口();
}其他{
标题(位置: \".$links['status'].\"?\".urlen$c$c(\"item=incident&action=view&status=-1&place=\".$links['home']));
}
?>
这导致寄来的文件,但是当我打开它,我收到一个错误说:
文件类型的纯文本文件(text / plain的),不支持
首先,我想通过检查HTTP标头开始。您可以在Firefox中做到这一点很容易使用实时HTTP头扩展名;不知道在副手其他浏览器当量。这将让你验证,如果头实际上是设置成应用程序/ PDF,以及是否你的另一头越来越设置为好。
如果没有头越来越设置,你可能会调用之前无意中发送输出到头()
。有之前的任何空格的<?PHP
标签
If I goto http://site.com/uploads/file.pdf I can retrieve a file.
However, if I have a script such as:
<?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']));
}
?>
This results in sending me the file, but when I open it I receive an error saying:
"File type plain text document (text/plain) is not supported"
First off, I'd start by checking the HTTP headers. You can do this in Firefox easily using the "Live HTTP headers" extension; not sure about equivalents in other browsers offhand. This will let you verify if the header is actually getting set to "application/pdf" and whether your other headers are getting set as well.
If none of the headers are getting set, you might be inadvertently sending output before the calls to header()
. Is there any whitespace before the <?php
tag?
这篇关于通过PHP服务文件的用户通过HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!