2011-07-11 31 views
2

下面我有一個簡單的聲明:file_exists在PHP代碼

if(file_exists('/images/alum/'.$profile['pic'])) 
    echo '/images/alum/'.$profile['pic']; 
    else echo '/images/user_default.jpg'; 

的文件是存在的,但它總是會默認圖像。我錯了什麼?

+1

你確定你的路徑正確嗎?試試'。/ images/alum /'... – wanovak

+0

這樣做。謝謝! – TH1981

+0

我建議做一些關於相對路徑和絕對路徑的閱讀 - 文件路徑(駐留在文件系統上的文件的路徑)和URL路徑(與''和其他HTML標記一起使用的地址)。閱讀完它後,您應該瞭解並能夠告訴何時(以及爲什麼)使用'./ images /','images /'或'/ images /'。 – binaryLV

回答

1

您在服務器上表示文件系統根目錄下的文件存在。您將有可能增加一些...

0

嘗試:

if(file_exists('./images/alum/'.$profile['pic'])) 
echo '/images/alum/'.$profile['pic']; 
else echo '/images/user_default.jpg'; 

即改變「/圖片」到「./images」,但只有在file_exists調用。

0

將其更改爲此。

if(file_exists('./images/alum/'.$profile['pic'])) 
    echo './images/alum/'.$profile['pic']; 
else 
    echo './images/user_default.jpg'; 
0

確保$profile['pic']具有有效的文件名,與路徑收縮是有效的,並與PARTH指向文件的當前目錄...

臨時否定條件看,從配置文件...