2012-05-26 98 views
0

我已经使用这种编码风格将文件放置在我的MYSQL数据库上。我很确定它存储正确正确的PHP头文件用于下载存储的文件

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) 
{ 
$fileName = $_FILES['userfile']['name']; 
$tmpName = $_FILES['userfile']['tmp_name']; 
$fileSize = $_FILES['userfile']['size']; 
$fileType = $_FILES['userfile']['type']; 

$fp  = fopen($tmpName, 'r'); 
$content = fread($fp, filesize($tmpName)); 
$content = addslashes($content); 
fclose($fp); 

if(!get_magic_quotes_gpc()) 
{ 
    $fileName = addslashes($fileName); 
} 

但我无法从数据库中下载它,而我的头是错误的。

$resumename = get_the_author_meta('resumename', $current_user->ID); 
$resumetype = get_the_author_meta('resumetype', $current_user->ID); 
$resumesize = get_the_author_meta('resumesize', $current_user->ID); 
$content = get_the_author_meta('resumecontent', $current_user->ID); 

header("Content-Length: $resumesize"); 
header("Content-Type: $resumetype"); 
header("Content-Disposition: attachment; filename=$resumename"); 

echo($content); 

目前,它返回的页面(它使用的头加载的实际页面的数据,而不是文件数据)的文件已损坏。

任何人都知道正确的标题用于此?我已经尝试了@readfile,它更糟。文件类型主要是doc,pdf,rtf。谢谢你的帮助!

+0

为什么你不打印出你正在发送调试的头文件? - 只是为了看看是否发送了合适的头文件。另外我认为你至少应该添加Content-Transfer-Encoding:二进制头文件 – Andreas

回答

0
header("Content-Description: File Transfer"); 
header("Content-Disposition: attachment; filename=$resumename"); 
header("Content-Type: $MIME_Type"); //you can use application/pdf for PDF 
header("Content-Transfer-Encoding: binary");