file_exists()とは? わかりやすく解説

Weblio 辞書 > コンピュータ > PHP関数リファレンス > file_exists()の意味・解説 

file_exists

(PHP 4, PHP 5)
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 メソッド

指定したファイル存在するかどうか確認します

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説

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
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「file_exists()」の関連用語

file_exists()のお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



file_exists()のページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
PHP Documentation GroupPHP Documentation Group
Copyright © 1997 - 2025 by the PHP Documentation Group.
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS