2012-09-22 57 views
0

我下載文件的響應,但它提供了無效的文件的回報。嘗試下載文件並獲取無效的文件在PHP核心

這裏是我的download_content.php

<?php  
    $filename = $_GET["filename"]; 


    $buffer = file_get_contents($filename); 

    /* Force download dialog... */ 
    header("Content-Type: application/force-download"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Type: application/download"); 

    /* Don't allow caching... */ 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

    /* Set data type, size and filename */ 
    header("Content-Type: application/octet-stream"); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: " . strlen($buffer)); 
    header("Content-Disposition: attachment; filename=$filename"); 

    /* Send our file... */ 
    echo $buffer; 
?> 

下載文件鏈接:

<a href="download_content.php?filename=/gallery/downloads/poster/large/'.$r['file'].'"> Download</a> 

$r['file']包含要下載的文件名。

包含文件的文件夾的完整路徑是:

localhost/ja/gallery/downloads/poster/large/'.$r['file'].' 

jahtdocs根文件夾。

我不知道實際的問題是什麼,誰能幫助我嗎?

回答

0

你$文件名變量包含整個路徑,如下

header("Content-Disposition: attachment; filename=$filename"); 

做這樣

$newfilename = explode("/",$filename); 
$newfilename = $newfilename[count($newfilename)-1]; 

$fsize = filesize($filename); 

Then pass new variable into header 

header("Content-Disposition: attachment; filename=".$newfilename); 
header("Content-length: $fsize"); 

//newline added as below 
ob_clean(); 
flush(); 
readfile($filename); 
+0

仍然顯示無效文件... – user1688258

+0

你能否粘貼整個錯誤你究竟得到什麼?你是否將文件內容存入緩衝區變量? – GBD

+0

耶即時得到文件全路徑名$ ..但是當我們點擊下載鏈接它返回一個無效的圖片實際的圖像大小爲459KB,但它給420byte文件的回報... – user1688258

1

正如在其他提問時表示,這樣看起來更好:

$filename = $_GET["filename"]; 
// Validate the filename (You so don't want people to be able to download 
// EVERYTHING from your site...) 

// For example let's say that you hold all your files in a "download" directory 
// in your website root, with an .htaccess to deny direct download of files. 
// Then: 

$filename = './download' . ($basename = basename($filename)); 

if (!file_exists($filename)) 
{ 
    header('HTTP/1.0 404 Not Found'); 
    die(); 
} 
// A check of filemtime and IMS/304 management would be good here 
// Google 'If-Modified-Since', 'If-None-Match', 'ETag' with 'PHP' 

// Be sure to disable buffer management if needed 
while (ob_get_level()) { 
    ob_end_clean(); 
} 

Header('Content-Type: application/download'); 
Header("Content-Disposition: attachment; filename=\"{$basename}\""); 
header('Content-Transfer-Encoding: binary'); // Not really needed 
Header('Content-Length: ' . filesize($filename)); 
Header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 

readfile($filename); 

那說,「無效文件」是什麼意思?長度不好?零長度?錯誤的文件名?錯誤的MIME類型?錯誤的文件內容?你的眼睛裏的一切都可以清楚地表達出你的意思,但是從我們的結尾來看,這很不明顯。

UPDATE:顯然沒有找到文件,這意味着參數PHP腳本是錯誤的(指這是不是有文件)。修改上面的代碼以允許目錄包含所有文件,並從那裏下載。

+0

這幫了我很多。謝謝 – NetEmmanuel

1
<?php 
    header("Content-Type: application/vnd.ms-excel"); 
    header("Content-disposition: attachment; filename=spreadsheet.xls"); 

    // print your data here. note the following: 
    // - cells/columns are separated by tabs ("\t") 
    // - rows are separated by newlines ("\n") 

    // for example: 
    echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n"; 
    echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n"; 
    ?> 
相關問題