14
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPのファイル入出力

Posted at

読込

ファイルを文字列として読み込む
http://php.net/manual/ja/function.file-get-contents.php

// ファイルから読み込む
$string = file_get_contents("hoge.txt");

// URLから読み込む
$string = file_get_contents("http://hoge.com/");

ファイルを配列として読み込む
http://php.net/manual/ja/function.file-get-contents.php

// ファイルから読み込む
$array = file("hoge.txt");

// URLから読み込む
$array = file("http://hoge.com/");

書込

ファイルに書き込む
http://www.php.net/manual/ja/function.file-put-contents.php
fopen()、 fwrite()、 fclose() を実行するのと等価

 file_put_contents("hoge.txt", "piyo");
 file_put_contents("hoge.txt", $array);

// ファイルに追記
 file_put_contents("hoge.txt", "piyo", FILE_APPEND);

// 排他ロック
 file_put_contents("hoge.txt", "piyo", LOCK_EX);
14
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?