2016-07-16 148 views
0

我正在嘗試製作一個程序,它允許您上傳2個文件,運行一些代碼,然後創建一個新文件並將其下載到新數據中。PHP創建文件並下載文件

我搜索過了,它看起來像我一樣,但它不會創建一個文件,它只是在網站上打印文本。

這是代碼。

header("Content-type: text/plain"); 
    header("Content-Disposition: attachment; filename=unique_ips.txt"); 
    echo 'IP addresses only found in file A:' . "\n"; 
    echo $array_a_result . "\n\n"; 
    echo 'IP addresses only found in file B:' . "\n"; 
    echo $array_b_result; 
    exit(); 

它似乎並不關心第二個標題。

+0

這裏運行的是哪裏?如果它沒有在其他輸出之前發送,那麼它將不起作用 - 報頭輸出必須是發送的第一個輸出。 –

+0

你有沒有嘗試過爲你的Content-type使用「application/octet-stream」? – Terry

回答

0

切記不要在header()之前回應任何內容。我只是測試了下面的代碼。應該按預期工作!

<?php 
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename=myfile.txt'); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
flush(); 
echo 'IP addresses only found in file A:' . "\n"; 
echo $array_a_result . "\n\n"; 
echo 'IP addresses only found in file B:' . "\n"; 
echo $array_b_result; 
+0

請解釋爲什麼當你downvote! – Iceman

0

沒有辦法想象你的就是你發佈的代碼纔去上什麼,但是,它會建議,如果你正在做一個下載,你要小心,不要輸出任何文本(甚至白色空間),直到部分代碼完全運行。在<?php之前甚至有一個空格會引發錯誤。這裏是一個函數,你可能想嘗試一下(如果你願意):

<?php 

     /** 
     * FULL-NAME OF THE FILE TO DOWNLOAD 
     * @param $downloadFileName 
     * NEW-NAME FOR THE FILE... DO NOT USE EXTENSIONS LIKE .pdf OR .doc OR .txt 
     * @param null $newFileName 
     * @return bool 
     * 
     */ 
     function processDownload($downloadFileName, $newFileName=null) { 
      $ext    = pathinfo($downloadFileName, PATHINFO_EXTENSION); 
      if(!$newFileName){ 
       $newFileName = basename($downloadFileName); 
      }else{ 
       $newFileName .= "." . $ext; 
      } 
      if(file_exists($downloadFileName)){ 
       $size  = @filesize($downloadFileName); 
       header('Content-Description: File Transfer'); 
       header('Content-Type: application/octet-stream'); 
       header('Content-Disposition: attachment; filename=' . $newFileName); 
       header('Content-Transfer-Encoding: binary'); 
       header('Connection: Keep-Alive'); 
       header('Expires: 0'); 
       header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
       header('Pragma: public'); 
       header('Content-Length: ' . $size); 
       readfile($downloadFileName); 
       return TRUE; 
      } 
      return FALSE; 
     } 

如果您選擇嘗試這個功能,建議,使之成爲第一個函數調用在腳本這樣的頂部:

<?php 
     // NOTICE THAT THERE IS NO WHITE-SPACE BEFORE PHP. 
     // AND NO OUTPUT WAS SENT (IE: NOTHING ECHOED OUT...) 

     processDownload(__DIR__ . "/downloads/unique_ips.txt", "Unique-IPs"); 

     // FROM HERE ON, YOU MAY ECHO WHATEVER YOU WISH... 
     // echo $whatever; 
     // echo $youWish; 
     // echo $may; 
     // echo $nowBeEchoed; 
     // echo $afterCalling; 
     // echo $theFunctionAbove; 

     echo 'IP addresses only found in file A:' . "\n"; 
     echo $array_a_result . "\n\n"; 
     echo 'IP addresses only found in file B:' . "\n"; 
     echo $array_b_result; 


     function processDownload($downloadFileName, $newFileName=null) { 
      $ext    = pathinfo($downloadFileName, PATHINFO_EXTENSION); 
      if(!$newFileName){ 
       $newFileName = basename($downloadFileName); 
      }else{ 
       $newFileName .= "." . $ext; 
      } 
      if(file_exists($downloadFileName)){ 
       $size  = @filesize($downloadFileName); 
       header('Content-Description: File Transfer'); 
       header('Content-Type: application/octet-stream'); 
       header('Content-Disposition: attachment; filename=' . $newFileName); 
       header('Content-Transfer-Encoding: binary'); 
       header('Connection: Keep-Alive'); 
       header('Expires: 0'); 
       header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
       header('Pragma: public'); 
       header('Content-Length: ' . $size); 
       readfile($downloadFileName); 
       return TRUE; 
      } 
      return FALSE; 
     }