2012-02-09 57 views
8

需要codeigniter幫助,我認爲file_exists是用於服務器路徑,而不是url。但我的圖像文件夾與應用程序和系統文件夾的級別相同。在谷歌上找不到任何東西,請幫忙

$url=base_url(); 
$filename="$url/upload/$id.jpg"; 
if (file_exists($filename)){ 
    echo "<li><img src=\"".$url."upload/$id.jpg\" width=\"40\" height=\"40\" /></li>"; 
}else{ 
    echo "<li><img src=\"http://www.mydomain.com/haha/image/noimg.png\" width=\"40\" height=\"40\" /></li>"; 
} 
+2

一件事,file_exists * *是服務器的路徑,而不是URL。所以,你不會用BASE_URL(),你會使用APPPATH – Ben 2012-02-09 21:04:05

回答

40

Codeigniter總是在index.php上運行,所以所有的路徑都是相對的。您可以使用任何以下的,假設upload/處於同一水平index.php

  • file_exists("upload/$id.jpg")

  • file_exists("./upload/$id.jpg")

  • file_exists(FCPATH."upload/$id.jpg")

FCPATH是一個常數,含有ab的Codeigniter集合通往您的index.php的溶質路徑。

作爲一個方面說明,我檢查文件時,如file_exists()返回目錄上真正喜歡is_file()。另外,您可能想要查看getimagesize()是否返回FALSE以確保您有圖像。

+0

這並不總是奏效。 – Linesofcode 2014-07-15 17:27:48

+0

我的數據具有圖像完全path..eg。(HTTP://localhost/media/newsdata/20150923/Entertainment/Casspture.PNG)當我使用file_exists($ thisfile)是撥錯,它總是返回false。 。請 – 2015-09-24 03:23:43

+0

只能使用服務器的路徑:'file_exists(「媒體/ newsdata/20150923 /娛樂/ Casspture.PNG」)'如果 – 2015-09-24 04:03:50