![](/img/trans.png)
[英]How can I display an image on webpage if it exists…else use “default image”?
[英]File Exists? Display image else display default image
我似乎無法使以下代碼正常工作?
$playerfileLocation = "../wp-content/gallery/playerphotos/' . $playerid . '.jpg";
if(file_exits($playerfileLocation))
echo "The file File exists";
else
echo "The file File does not exist";
“ playerid”字段是通過的數字。
我似乎無法正常工作。 啊!
您的報價不匹配。 試試這個代碼。
$playerfileLocation = "../wp-content/gallery/playerphotos/" . $playerid . ".jpg";
if(file_exists($playerfileLocation))
echo "The file File exists";
else
echo "The file File does not exist";
更新 :實際上,我建議每當PHP看到雙引號時都使用以下代碼,它會嘗試解析它們之間的任何內容,在這種情況下,這不是必需的。 這是性能上的一個小優化。
$playerfileLocation = '../wp-content/gallery/playerphotos/' . $playerid . '.jpg';
if(file_exists($playerfileLocation))
echo "The file File exists";
else
echo "The file File does not exist";
另外,要檢查文件是否存在以及是否不顯示默認圖像,請使用以下代碼:
$playerfileLocation = '../wp-content/gallery/playerphotos/' . $playerid . '.jpg';
$defaultfileLocation = '../wp-content/gallery/playerphotos/default.jpg';
if (!file_exists($playerfileLocation)) {
$playerfileLocation = $defaultfileLocation;
}
您的代碼拼寫錯誤:(不存在)
$playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg";
$default_player = "../wp-content/gallery/playerphotos/default.jpg";
if(file_exits($playerfileLocation))
{
}
else
{
$playerfileLocation=$default_player;
}
在php中,我們也可以使用雙引號獲取變量值。 在路徑中使用雙引號是一個好習慣,因此不需要太多縮進。
$playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg";
$default_player = "../wp-content/gallery/playerphotos/default.jpg";
if(!file_exists($playerfileLocation))
{
$playerfileLocation=$default_player;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.