作成日:2018-07-19, 更新日:2018-08-23
基本
方法はいくつかある。
・file_exists() - ファイルが存在するかチェック
・is_readable() - ファイルが読み込みOKかチェック
・file_get_contents() - ファイルの中身を取得
・file_get_contents() - ファイルの中身を取得:1文字だけ取得
・fopen() - ファイルを開く
どれを使うべきか?
「file_get_contents() - ファイルの中身を取得」は他に比べると遅い。
「file_get_contents() - ファイルの中身を取得:1文字だけ取得」を使えば良さげ。
file_exists() - ファイルが存在するかチェック
if (file_exists($path)) { echo '存在する'; }
is_readable() - ファイルが読み込みOKかチェック
if (!is_readable($path)) { echo '存在しない'; }
file_get_contents() - ファイルの中身を取得
if (!@file_get_contents($path)) { echo '存在しない'; }
file_get_contents() - ファイルの中身を取得:1文字だけ取得
if (!@file_get_contents($path, NULL, NULL, 0, 1)) { echo '存在しない'; }
fopen() - ファイルを開く
if ( !@fopen($path, "r") ) { echo '存在しない'; }