PHPファイルを開く/読み取る/閉じる
この章では、サーバー上のファイルを開いたり、読み取ったり、閉じたりする方法について説明します。
PHPオープンファイル-fopen()
ファイルを開くためのより良い方法は、fopen()
関数を使用することです。この関数は、関数よりも多くのオプションを提供しますreadfile()
。
レッスンでは、テキストファイル「webdictionary.txt」を使用します。
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
の最初のパラメーターにfopen()
は、開くファイルの名前が含まれ、2番目のパラメーターには、ファイルを開くモードを指定します。次の例では、fopen()関数が指定されたファイルを開くことができない場合にもメッセージを生成します。
例
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
ヒント:とfread()
機能については、fclose()
以下で説明します。
ファイルは、次のいずれかのモードで開くことができます。
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a+ | Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
PHP読み取りファイル-fread()
このfread()
関数は、開いているファイルから読み取ります。
の最初のパラメーターにfread()
は、読み取るファイルの名前が含まれ、2番目のパラメーターには、読み取る最大バイト数が指定されます。
次のPHPコードは、「webdictionary.txt」ファイルを最後まで読み取ります。
fread($myfile,filesize("webdictionary.txt"));
PHPクローズファイル-fclose()
このfclose()
関数は、開いているファイルを閉じるために使用されます。
プログラミングが終了したら、すべてのファイルを閉じることをお勧めします。サーバー上で開いているファイルが実行されてリソースを消費することは望ましくありません。
にはfclose()
、閉じたいファイルの名前(またはファイル名を保持する変数)が必要です。
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHPの単一行の読み取り-fgets()
このfgets()
関数は、ファイルから1行を読み取るために使用されます。
次の例では、「webdictionary.txt」ファイルの最初の行を出力します。
例
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
注:関数の呼び出し後fgets()
、ファイルポインターは次の行に移動しました。
PHPのファイルの終わりを確認します--feof()
このfeof()
関数は、「ファイルの終わり」(EOF)に到達したかどうかをチェックします。
このfeof()
関数は、長さが不明なデータをループする場合に役立ちます。
次の例では、ファイルの終わりに達するまで、「webdictionary.txt」ファイルを1行ずつ読み取ります。
例
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
PHPの単一文字の読み取り-fgetc()
このfgetc()
関数は、ファイルから1文字を読み取るために使用されます。
次の例では、ファイルの終わりに達するまで、「webdictionary.txt」ファイルを1文字ずつ読み取ります。
例
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
注:関数を呼び出した後fgetc()
、ファイルポインターは次の文字に移動します。
完全なPHPファイルシステムリファレンス
ファイルシステム関数の完全なリファレンスについては、完全な PHPファイルシステムリファレンスにアクセスしてください。