2015-12-21 74 views
3

我想下載一個.pptx文件從我的站點文件夾下載或獲取保存彈出,用戶可以選擇保存文件夾。但是當我打電話給我的功能時,它在console.log中打印了一個內容。如何從服務器或本地xampp下載文件? php

我已經使用不同的代碼,但沒有人工作。

PK����Uy�G��-j����P����_rels/.rels���������������������JA��>Ő{w�D��^D�Md}�0���;3!�}{���B)��������l>Hʔ��uӂ��s�����{Z݃)�)��98Q��n�B3j])���TF*FU~����"�&3�:��D�Z�`�d7m{g�'�Ls��`����6�[email protected]��Њ�n�N�ӡ�B�ϵ]Ή���^��^(����1��HI/y�Q) 

ו-2 jY9%dPKUyGI + docProps /core.xml m j @E ;-; RL J! 4 3 ����wb���R�sW���O�gq1�5-T(Z��ߞ�'�DM�f���e��~G�����c⬎�*� =�Ϊ�G:�7�"��3fo��y�d�ˌ���}D�j�Q�Wa�V#�+-�����Eyb A -..... 還有更多。

這是我的代碼:

 function downloadProject($file, $db) 
     { 
      $path = "../assets/convertables/Presentaties/"; // change the path to fit your websites document structure 
      $dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', $file.'.pptx'); // simple file name validation 
      $dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters 
      $fullPath = $path.$dl_file; 
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
      header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation'); 
      header("Content-Transfer-Encoding: Binary"); 
      header("Content-length: ".filesize($fullPath)); 
      header("Content-disposition: attachment; filename=\"".$dl_file."\""); 
      readfile($fullPath);   
     } 

編輯:我已經檢查與解答其他問題,但並沒有爲我工作!

+0

我怎樣才能投票你的評論.... – TheJokerAeZ

+0

有沒有辦法@TheJokerAez,我會刪除這個不要擔心 – devpro

回答

0

你可以試試這個:

首先,你需要定義下載文件

的正確的MIME類型/內容類型在你的情況是:應用程序/ vnd.ms - PowerPoint中

所以代碼會:

$mime = 'application/vnd.ms-powerpoint'; 

    // Generate the server headers 
    if(strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) //id browser is IE 
    { 
     header('Content-Type: "'. $mime .'"'); 
     header('Content-Disposition: attachment; filename="'.$filename.'"'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header("Content-Transfer-Encoding: binary"); 
     header('Pragma: public'); 
     header("Content-Length: ".filesize($data)); 
    } 
    else 
    { 
     header("Pragma: public"); 
     header("Expires: 0"); 
     header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
     header("Cache-Control: private", false); 
     header("Content-Type: ".$mime, true, 200); 
     header('Content-Length: '.filesize($data)); 
     header('Content-Disposition: attachment; filename='.$filename); 
     header("Content-Transfer-Encoding: binary"); 
    } 
    readfile($data); 
    exit; 

您可以瞭解更多的網站在http://www.phpdevtips.com/snippets/force-file-download-with-correct-content-type/

+0

下載文件在哪裏去?我的ajax請求發送成功,但我看不到任何下載。 – TheJokerAeZ

+0

哦。你正在使用ajax ...所以我有想法:你可以使用ajax打開新的瀏覽器窗口(或標籤)與充滿下載URL或使用document.location(download_url); – GiapLee

+0

我發送所選行名稱的ID,並在PHP中,我得到這個名稱與$ _post。我和班級和職能一起工作 – TheJokerAeZ

相關問題