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 fopen()関数

❮PHPファイルシステムリファレンス

ファイルを開き、行を読み取ります-EOFに達するまで:

<?php
$file = fopen("test.txt", "r");

//Output lines until EOF is reached
while(! feof($file)) {
  $line = fgets($file);
  echo $line. "<br>";
}

fclose($file);
?>

定義と使用法

fopen()関数は、ファイルまたはURLを開きます。

注:テキストファイルに書き込むときは、必ず正しい行末文字を使用してください。Unixシステムは\ nを使用し、Windowsシステムは\ r \ nを使用し、Macintoshシステムは行末文字として\ rを使用します。Windowsには、ファイルを操作するときに\ nを\ r \ nに変換する変換フラグ( 't')が用意されています。'b'を使用してバイナリモードを強制することもできます。これらのフラグを使用するには、モードパラメータの最後の文字として「b」または「t」のいずれかを指定します。

構文

fopen(filename, mode, include_path, context)

パラメータ値

Parameter Description
filename Required. Specifies the file or URL to open
mode Required. Specifies the type of access you require to the file/stream.

Possible values:

  • "r" - Read only. Starts at the beginning of the file
  • "r+" - Read/Write. Starts at the beginning of the file
  • "w" - Write only. Opens and truncates the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
  • "w+" - Read/Write. Opens and truncates the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
  • "a" - Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist
  • "a+" - Read/Write. Preserves file content by writing to the end of the file
  • "x" - Write only. Creates a new file. Returns FALSE and an error if file already exists
  • "x+" - Read/Write. Creates a new file. Returns FALSE and an error if file already exists
  • "c" - Write only. Opens the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
  • "c+" - Read/Write. Opens the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
  • "e" - Only available in PHP compiled on POSIX.1-2008 conform systems.
include_path Optional. Set this parameter to '1' if you want to search for the file in the include_path (in php.ini) as well
context Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream


技術的な詳細

戻り値: 成功時のファイルポインタリソース、FALSEおよび失敗時のエラー。関数名の前に「@」を追加すると、エラーを非表示にできます。
PHPバージョン: 4.3+
PHP変更ログ: PHP 7.1:「e」オプション
を追加PHP 5.2:「c」および「c +」オプションを追加

❮PHPファイルシステムリファレンス