我似乎无法使用以下代码?
$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"字段是一个可以通过的数字。
我似乎无法让它发挥作用。 Urgh!
答案 0 :(得分:5)
您的报价不匹配。试试这段代码。
$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;
}
答案 1 :(得分:1)
您的代码拼写错误:(不存在退出)
$playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg";
$default_player = "../wp-content/gallery/playerphotos/default.jpg";
if(file_exits($playerfileLocation))
{
}
else
{
$playerfileLocation=$default_player;
}
答案 2 :(得分:0)
在php中,我们也可以用双引号获取变量值。在路径中使用双引号是一种好习惯,因此不需要太多缩进。
$playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg";
$default_player = "../wp-content/gallery/playerphotos/default.jpg";
if(!file_exists($playerfileLocation))
{
$playerfileLocation=$default_player;
}