カレントディレクトリに”test.txt“と言うファイルが存在するか調べる。
存在すれば「test.txt is exists.」、存在しなければ「test.txt is not exists.」と表示する。
<?php
$filename = "test.txt";
if (file_exists($filename)) {
echo $filename . " is exists.";
} else {
echo $filename . " is not exists.";
}
?>
以下、実行結果。
D:\Cani\php>dir
ドライブ D のボリューム ラベルは ボリューム です
ボリューム シリアル番号は E0DB-6441 です
D:\Cani\php のディレクトリ
2013/06/02 01:20 <DIR> .
2013/06/02 01:20 <DIR> ..
2013/06/02 00:48 156 fileexist.php
1 個のファイル 156 バイト
2 個のディレクトリ 1,890,692,272,128 バイトの空き領域
D:\Cani\php>php fileexist.php
test.txt is not exists.
D:\Cani\php>echo > test.txt
D:\Cani\php>dir test.txt
ドライブ D のボリューム ラベルは ボリューム です
ボリューム シリアル番号は E0DB-6441 です
D:\Cani\php のディレクトリ
2013/06/02 01:20 21 test.txt
1 個のファイル 21 バイト
0 個のディレクトリ 1,890,692,272,128 バイトの空き領域
D:\Cani\php>php fileexist.php
test.txt is exists.
D:\Cani\php>