2013-01-03 120 views
-1

如果第6行(.include ...)被取消註釋,那麼以下腳本將失敗 - 無論文件「somefile.php」的內容是什麼。即使是一個0字節(空)的文件。我試圖移動這些函數並創建一個「utils.php」庫,這是PHP世界中的一個不容忽視的問題嗎? Error_log是空的。我在Linux 2.6.32上使用PHP 5.2.17。謝謝。爲什麼包含空文件會導致PHP腳本失敗?

<?php // File: index.php 
error_reporting (E_ALL); 
ini_set ('display_errors', 1); 

//Uncomment the following line and the script fails 
//.include "somefile.php"; 

function imageToBrowser ($pseudonym, $filePath) { 
     header("Pragma: public"); 
     header("Expires: 0"); 
     header('Cache-Control: no-store, no-cache, must-revalidate'); 
     header('Cache-Control: pre-check=0, post-check=0, max-age=0', false); 
     header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT'); 
     header("Content-Length: ".(string)(filesize($filePath))); 
     header('Content-Type: application/x-download'); 
     header('Content-Disposition: attachment; filename="'.$pseudonym.'.png"'); 
     header('Content-Transfer-Encoding: binary'); 

     if ($file = fopen($filePath, 'rb')) { 
       while(!feof($file) and (connection_status()==0)) { 
         print(fread($file, filesize($filePath))); 
         flush(); 
       } 
       fclose($file); 
}} 

if (isset($_GET['showImage']) && ($imageMask = $_GET['showImage'])) { 
     imageToBrowser ($imageMask, 'Clear64.png'); // Use any .png file in $cwd 
} else { 
     echo "<html><body><center>\n"; 
     echo "<img border='0' src=\"{$_SERVER['PHP_SELF']}?showImage=365\" alt=\"Missing Image\"/>\n"; 
     echo "</center></body></html>"; 
} 
?> 
+2

什麼正在發生的確切的錯誤?說它不起作用並不能告訴我們爲什麼。 –

+2

希望在取消註釋時,還可以在'include'之前刪除點。 – zneak

+0

頁面爲空(空白)。沒有錯誤信息,當然也不是我用這行註釋得到的圖像。 –

回答

1

包含前應該沒有.。 Dot是PHP中的連接運算符,在這裏無關緊要。

另請注意,您的ini_set ('display_errors', 1);聲明不會有助於解析錯誤,原因很明顯。更好地在php.ini或網絡服務器配置中設置您的設置。之前有說法

的require()函數是相同的包括(),但它處理錯誤不同

0

首先移除點。如果發生錯誤,include()函數將生成警告,但腳本將繼續執行。 require()生成一個致命錯誤,腳本將停止。

類似的require_once()語句與require()完全相同,除非PHP會檢查文件是否已經包含在內,如果是,則不包括(需要)它。

所以請這將是更好地爲您所用

require_once('somefile.php'); 
+2

請不要縮進答案中的所有文字。這樣做會導致它打印出未包裝的等寬文本,就像它是程序代碼一樣。 –

+2

像這樣使用括號可以使'require_once'看起來像一個函數調用,而不是。 – alex

相關問題