2014-02-19 88 views
0

第一個目標:建立一個index.html文件,並創建一個鏈接來下載生成的文件生成html文件,並用PHP下載

這裏的問題是,當我點擊生成新的文件下載的文件總是相同的,心不是更新

變量$content具有INSITE與<headers><aside> and <sections>

整個HTML頁面,我有以下代碼

if(empty($error)){ 
     echo "<h3>File generated</h3>"; 
     $my_file = 'index.html'; 
     if (file_exists($my_file)) { 
      if(unlink($my_file)){ 
      }; 
      $new_file = 'index.html'; 
      $handle = fopen($new_file, 'w') or die('Cannot open file: '.$new_file); 
      $data = $content; 
      fwrite($handle, $data); 
      fclose($handle); 

      echo "<a download='index.html' href='index.html'><b class='download'>Download</b></a>"; 
     } else { 
      $new_file = 'index.html'; 
      $handle = fopen($new_file, 'w') or die('Cannot open file: '.$new_file); 
      $data = $content; 
      fwrite($handle, $data); 
      fclose($handle); 
      echo "<a download='index.html' href='index.html'><b class='download'>Download</b></a>"; 
     } 
+0

您是否有權寫入該文件? – MSadura

+0

是的,該文件已創建,但是當我生成新文件並單擊下載時,下載的文件是第一個創建的。如果我檢查服務器上新文件的內容,它已更新。 –

+1

如果你手動刪除文件,然後嘗試下載文件,你會得到錯誤或仍然可以下載它? – MSadura

回答

0

嗨,我認爲這將是工作,如果變量$內容將包含正確的數據,並且您有權限讀取,更新,創建文件。在代碼中,我使用的減少你的代碼

if(empty($error)){ 
    echo "<h3>File generated</h3>"; 
    $my_file = 'index.html'; 
    file_put_contents($my_file, $content); 
    echo "<a download='index.html' href='index.html'><b class='download'>Download</b></a>"; 
} 

UPDATE file_put_contents功能: 你確定你已經正確生成內容和$內容總是包含不同的價值?

+0

是,我在服務器上檢查了manualy文件,並且allways包含不同的值 –

+0

如果您沒有看到更改,您可以手動將某些內容添加到文件index.html並查看瀏覽器上的更改,也許您錯誤的文件或URL – Jack