file_exists
file_exists — ファイルまたはディレクトリが存在するかどうか調べる
説明
bool file_exists ( string filename )filenameで指定したファイルまたはディレクトリ が存在すればTRUEを返し、そうでなければFALSEを返します。
Windows上でネットワーク共有上のファイルを調べるには、 //computername/share/filenameまたは \\computername\share\filenameのように指定してください。
例 596. あるファイルが存在するかどうか調べる
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
print "The file $filename exists";
} else {
print "The file $filename does not exist";
}
?>
注意: この関数の結果は キャッシュされます。詳細は、clearstatcache() を参照してください。
ティップ
PHP 5.0.0 以降、この関数は、 何らかの URL ラッパーと組合せて使用することができます。 どのラッパーが stat() ファミリーをサポートしているか のリストについては、付録 M. サポートされるプロトコル/ラッパー を参照してください。警告 |
この関数は セーフモード
の制限のためファイルにアクセスできない場合 FALSE を返します。
しかし safe_mode_include_dir
で指定されたディレクトリに存在する場合は
included
することができます。
|
is_readable(), is_writable(), is_file(), file()も参照して下 さい。
File.Exists メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim path As String Dim returnValue As Boolean returnValue = File.Exists(path)
戻り値
呼び出し元が必要なアクセス許可を持ち、path に既存のファイル名が格納されている場合は true。それ以外の場合は false。path が null 参照 (Visual Basic では Nothing)、無効なパス、または長さ 0 の文字列の場合にも、このメソッドは false を返します。呼び出し元が指定したファイルを読み取るための十分なアクセス許可を持たない場合、例外はスローされず、このメソッドは、path の有無にかかわらず false を返します。

Exists メソッドは、パスの検証には使用できません。このメソッドは、path に指定されたファイルが存在するかどうかをチェックするだけです。Exists に無効なパスが渡されると、false が返されます。
Exists メソッドを呼び出してから、ファイルに Delete などの操作を実行するまでの間に、他のプロセスがこのファイルに対して何らかの操作を実行する可能性があることに注意してください。例に示すように、Exists メソッドとファイルに対して実行する操作を try...catch ブロック内にラップすることをお勧めします。これにより、競合が発生する可能性のある範囲を狭めることができます。Exists メソッドはファイルを確実に利用できる可能性を高めるだけで、保証するわけではありません。
path パラメータは、相対パス情報または絶対パス情報を指定することを許可されています。相対パス情報は、現在の作業ディレクトリに対して相対的に解釈されます。現在の作業ディレクトリを取得するには、GetCurrentDirectory のトピックを参照してください。
path がディレクトリを示す場合、このメソッドは false を返します。ファイルが存在するかどうかを確認する前に、文字列の末尾の空白は path パラメータから削除されます。

Exists メソッドを使用して、ファイルが上書きされないようにする例を次に示します。
Imports System Imports System.IO Imports System.Text Public Class Test Public Shared Sub Main() Dim path As String = "c:\temp\MyTest.txt" Dim path2 As String = path + "temp" Try Dim sw As StreamWriter = File.CreateText(path) sw.Close() ' Do the Copy operation only if the first file exists ' and the second file does not. If File.Exists(path) Then If File.Exists(path2) Then Console.WriteLine("The target file already exists.") Else 'try to copy it File.Copy(path, path2) Console.WriteLine("{0} was copied to {1}.", path, path2) End If Else Console.WriteLine("The source file does not exist.") End If Catch e As Exception Console.WriteLine("The process failed: {0}", e.ToString()) End Try End Sub End Class
using System; using System.IO; class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; string path2 = path + "temp"; try { using (StreamWriter sw = File.CreateText(path)) {} // Only do the Copy operation if the first file exists // and the second file does not. if (File.Exists(path)) { if (File.Exists(path2)) { Console.WriteLine("The target already exists"); } else { // Try to copy the file. File.Copy(path, path2); Console.WriteLine("{0} was copied to {1}.", path, path2); } } else { Console.WriteLine("The source file does not exist."); } } catch { Console.WriteLine("Double copying is not allowed, as expected."); } } }
using namespace System; using namespace System::IO; int main() { String^ path = "c:\\temp\\MyTest.txt"; String^ path2 = String::Concat( path, "temp" ); try { StreamWriter^ sw = File::CreateText( path ); if ( sw ) delete (IDisposable^)sw; // Only do the Copy operation if the first file exists // and the second file does not. if ( File::Exists( path ) ) { if ( File::Exists( path2 ) ) { Console::WriteLine( "The target already exists" ); } else { // Try to copy the file. File::Copy( path, path2 ); Console::WriteLine( "{0} was copied to {1}.", path, path2 ); } } else { Console::WriteLine( "The source file does not exist." ); } } catch ( Exception^ ) { Console::WriteLine( "Double copying is not allowed, as expected." ); } }
import System.*; import System.IO.*; class Test { public static void main(String[] args) { String path = "c:\\temp\\MyTest.txt"; String path2 = path + "temp"; try { StreamWriter sw = File.CreateText(path); try { } finally { sw.Dispose(); } // Only do the Copy operation if the first file exists // and the second file does not. if (File.Exists(path)) { if (File.Exists(path2)) { Console.WriteLine("The target already exists"); } else { // Try to copy the file. File.Copy(path, path2); Console.WriteLine("{0} was copied to {1}.", path, path2); } } else { Console.WriteLine("The source file does not exist."); } } catch (System.Exception exp) { Console.WriteLine("Double copying is not allowed, as expected."); } } //main } //Test


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- fileexistsのページへのリンク