文件存在吗?显示图像,否则显示默认图像


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;
}

您的代码拼写错误:(exist not exits)

$playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg";
$default_player = "../wp-content/gallery/playerphotos/default.jpg";
if(file_exits($playerfileLocation)) 
{
}
else
{
$playerfileLocation=$default_player;
}

在php中,我们也可以在双引号中获得变量值。在path中使用双引号是很好的做法,所以不需要太多的缩进。

$playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg";

$default_player = "../wp-content/gallery/playerphotos/default.jpg";
if(!file_exists($playerfileLocation)) 
{
$playerfileLocation=$default_player;
}