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:
|
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ファイルシステムリファレンス