PHPチュートリアル

PHPホーム PHPイントロ PHPインストール PHP構文 PHPコメント PHP変数 PHPエコー/印刷 PHPデータ型 PHP文字列 PHP番号 PHP数学 PHP定数 PHP演算子 PHP If ... Else ... Elseif PHPスイッチ PHPループ PHP関数 PHP配列 PHPスーパーグローバル PHP正規表現

PHPフォーム

PHPフォーム処理 PHPフォームの検証 PHPフォームが必要 PHPフォームのURL / Eメール PHPフォームの完了

PHP Advanced

PHPの日付と時刻 PHPインクルード PHPファイルの処理 PHPファイルのオープン/読み取り PHPファイルの作成/書き込み PHPファイルのアップロード PHPクッキー PHPセッション PHPフィルター PHPフィルターアドバンスト PHPコールバック関数 PHP JSON PHPの例外

PHPOOP _

PHPOOPとは PHPクラス/オブジェクト PHPコンストラクター PHPデストラクタ PHPアクセス修飾子 PHPの継承 PHP定数 PHP抽象クラス PHPインターフェース PHPの特性 PHP静的メソッド PHPの静的プロパティ PHP名前空間 PHPIterables

MySQLデータベース

MySQLデータベース MySQLコネクト MySQL Create DB MySQLテーブルの作成 MySQLの挿入データ MySQLは最後のIDを取得します MySQL Insert Multiple MySQLを準備しました MySQL Select Data MySQL Where MySQL Order By MySQLデータの削除 MySQLアップデートデータ MySQL制限データ

PHP XML

PHPXMLパーサー PHPSimpleXMLパーサー PHPSimpleXML-取得 PHP XMLExpat PHP XML DOM

PHP -AJAX

AJAXイントロ AJAX PHP AJAXデータベース AJAX XML AJAXライブ検索 AJAXポール

PHPの

PHPの例 PHPコンパイラ PHPクイズ PHP演習 PHP証明書

PHPリファレンス

PHPの概要 PHP配列 PHPカレンダー PHPの日付 PHPディレクトリ PHPエラー PHP例外 PHPファイルシステム PHPフィルター PHP FTP PHP JSON PHPキーワード PHP Libxml PHPメール PHP数学 PHPその他 PHP MySQLi PHPネットワーク PHP出力制御 PHP正規表現 PHP SimpleXML PHPストリーム PHP文字列 PHP変数の処理 PHPXMLパーサー PHP Zip PHPタイムゾーン

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ファイルシステムリファレンスにアクセスしてください。


PHP演習

エクササイズで自分をテストする

エクササイズ:

ファイルを開き、ファイルの終わりまで一度に1文字ずつ出力する正しい構文を記述します。

$myfile = fopen("webdict.txt", "r");
while(!($myfile)) {
  echo ($myfile);
}