2014-03-19 28 views
1

我試圖讓.txt,.xml,.sql文件可以在同一頁面下載,就像.zip,.docx文件一樣彈出並詢問「你想下載這個文件嗎?「。 我研究並發現,這些文件的Windows默認選項在新瀏覽器中打開,但我只想在同一頁面下載。我附加了圖片,就像點擊那些.txt,.xml,.sql文件時需要彈出的圖片一樣。 enter image description here 這怎麼辦?任何人都有這個想法。彈出要下載的文件,點擊鏈接

enter image description here

+0

我只是想迫使在同一頁面下載等擴展名的文件。如果有人有想法,請分享。謝謝,:) –

回答

2
 I have corrected by my own. Just putting the code below will successfully pop up any files to ask whether to download or not. This is my new code. 
    The download.php contains the code below: 

     <?php 
     $filename = $_GET["file"]; 
     $contenttype = "application/force-download"; 
     header("Content-Type: " . $contenttype); 
     header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";"); 
     if($_GET['path'] == 'new_uploads') 
     readfile($_SERVER['DOCUMENT_ROOT']."50dl/admin_panel_new/assets/plupload/new_uploads/".$filename); 
     else 
     readfile($_SERVER['DOCUMENT_ROOT']."50dl/admin_panel_new/assets/plupload/assessment_uploads/".$filename); 
     exit(); 
     ?> 

And the file.php contains this code: 

<img src="<?php echo base_url();?>assets/plupload/assessment_uploads/<?php echo $files->file_name?>" title="DOWNLOAD IMAGE" height="100" width="100" onClick="window.location.href='<?php echo base_url();?>logos/download?file=<?php echo $files->file_name?>&path=assessment_uploads'" class="download"/> 

This will correctly sort out the current issue. 
1

你應該通過在要求的文件下載代碼的標頭。

$task = isset($_REQUEST['task']) ? $_REQUEST['task'] : ''; 
if ($task == 'download'){ 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($_REQUEST['file_name'])); 
    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($_REQUEST['file_name'])); 
    ob_clean(); 
    flush(); 
    readfile($_REQUEST['file_name']); 
    exit;} 
+0

感謝您的回答,但沒有運氣。我已經試過這個,但是這隻會下載.docx,.zip文件,但在新的瀏覽器中會打開圖像(擴展名爲.jpg,.gif,.png等),然後我們必須下載那個不是我的文件通緝。我只想讓彈出窗口詢問「下載文件」。 –

+0

我用這個來下載圖片。檢查你的內容類型的頭。它應該是應用程序/八位字節流 – user3434478

+0

是的,上面的代碼是可以下載圖像,但對於所有類型的擴展仍有問題。我需要能夠彈出來要求下載該文件,但不能直接在新瀏覽器中打開。但無論如何感謝您的答案。我解決了它。 :) –

2
You can use this and it will work like a charm . 
<?php 
     $filename = $_GET["file"]; 
     $contenttype = "application/force-download"; 
     header("Content-Type: " . $contenttype); 
     header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";"); 
     if($_GET['path'] == 'new_uploads') 
     readfile($_SERVER['DOCUMENT_ROOT']."50dl/admin_panel_new/assets/plupload/new_uploads/".$filename); 
     else 
     readfile($_SERVER['DOCUMENT_ROOT']."50dl/admin_panel_new/assets/plupload/assessment_uploads/".$filename); 
     exit(); 
     ?> 
相關問題