2011-12-24 36 views
0

次のコードが動作しないようですか?ファイルが存在しますか?画像を表示する他の画像を表示するデフォルト画像

$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」フィールドは、通過する番号です。

私はそれを動作させるように見えません。アー!

+0

が働いて、この相対パスですか? –

+0

いいえ...ページがif文に到達すると停止します。この「テスト」の下に表示されないテキストが表示されます。 – DoubleA

答えて

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

二重引用符の問題PHPが二重引用符を参照するたびに、PHPはその間にあるものを解析しようとしますが、この場合は不要です。 ** –

+0

PHPは二重引用符を見ていると、それを解析しようとしますが、時間はかかりますが(それほど時間はかかりませんが、まだ時間がかかります)。一重引用符を使用すると、PHPは解析する必要がなく、解析に要する時間を節約できます。 '$ playerfileLocation =" ../ wp-content/gallery/playerphotos/$ playerid.jpg ";'これを行うには、PHPを必要とするでしょう。この場合、文字列に変数はありません。文字列全体を解析します。また、コードはあまり読みにくくはありません。 – Virendra

+0

私は最後に問題を見つけました。私が使っていた道に関連していました。私は絶対パスを定義する必要があります: $ absolutepath = $ _SERVER {'DOCUMENT_ROOT'}。 'wp-content/gallery/playerphotos /'。 $ photoname。 '.jpg'; file_exists関数が正常に機能しました。 ;) – DoubleA

0

PHPで、我々はまた、二重引用符で変数の値を取得することができます。パスに二重引用符を使用することは良い習慣であり、インデントをあまり必要としません。

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


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

あなたのコードがスペルミスされています(終了していない存在)

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

$default_player = "../wp-content/gallery/playerphotos/default.jpg"; 

if(file_exits($playerfileLocation)) 

{ 

} 

else 

{ 

$playerfileLocation=$default_player; 

} 
+0

さて、私は馬鹿のように感じます。 問題を解決していただきありがとうございます。 – DoubleA