2010-08-31 92 views
2

如果我转到http://site.com/uploads/file.pdf我可以检索一个文件。通过php向用户提供文件通过php

但是,如果我有一个脚本,例如:

<?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

当你没有从代码中检索到“Content-Type”,“Content-Disposition”和“Content-Length”时,你会得到同样的错误,而是将其手动设置为该特定文件? – Robert 2010-08-31 15:59:46

回答

1

首先,我首先检查HTTP标题。您可以使用“Live HTTP标头”扩展轻松地在Firefox中执行此操作;不确定其他浏览器中的等价物。这将让你验证头是否真的被设置为“application/pdf”,以及你的其他头文件是否已经设置好。

如果没有设置标头,则可能会在调用header()之前无意中发送输出。 <?php标签之前是否有空格?

+0

上面更新了整个文件的头文件代码。 – Chris 2010-08-31 16:02:42

+0

虽然这不是一个标题问题,我意识到我有一个错字... readfile($文件)应该是readfile($ fileName),但直播的http头有帮助。谢谢并遗憾浪费时间。 – Chris 2010-08-31 16:12:37

0

您确定application/pdf是您的浏览器实际看到的标题吗?

您可以使用各种HTTP开发工具检查,例如Mac或Firebug for Firefox的HTTP Client

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); 
} 
+0

我已经尝试过应用程序/八位字节流,并导致相同的问题。 – Chris 2010-08-31 16:04:51

+0

您是否尝试在我的示例的相同顺序中发送标题? – 2010-08-31 16:20:15

0

尝试预先设置“error_reporting(0);”。我在http://php.net/readfile的评论中找到了这个(你从这个例子中得到了这个例子)。

另一件可能成为问题的事情是您的文件大小。过去有关于PHP5的问题(我们在这里讨论2005,所以我希望现在已经解决了)阅读文件> 2MB有困难。如果您的文件大小超过此值,您可能需要验证它是否读取整个文件。

+0

文件应该小于500KB,所以大小不应该是一个问题。 – Chris 2010-08-31 16:05:51