LoginSignup
0
0

More than 3 years have passed since last update.

PHPの基本操作②「ファイルの読み込み、ファイルの書き込み、XMLの読み込み、JSONの読み込み」

Last updated at Posted at 2021-04-22

参考書籍

よくわかるPHPの教科書 PHP7対応版

他記事リンク

ファイルの書き込み

file_put_contents(書き込みするファイルのパス, 書き込みする内容)
実行した結果の戻り値はtrue/falseが返却されます。
※パスに該当するファイルが存在しない場合、ファイルは作成されますが、ディレクトリが存在しない場合にはディレクトリは作成されません。

ファイルの書き込み
<?php
  $result = file_put_contents('./sample_directory/sample.txt', 'file_put_contents関数でファイルに書き込みました');
  if ($result) {
    print('ファイルへの書き込みが完了しました。');
  } else {
    print('ファイルへの書き込みに失敗しました。');
  }
?>

ファイルの読み込み

file_get_contents(読み込みするファイルのパス)
実行した結果の戻り値はファイルの内容です。

ファイルの読み込み
<?php
  $contents = file_get_contents('./sample_directory/sample_read.txt');
  print($contents);
?>

読み込んだ内容をそのまま表示する場合には、readfile(ファイルのパス)でfile_get_contents関数とprint関数を両方実行した処理になります。

ファイルの追記

ファイルの読み込みとファイルの書き込みをともに実行することで、ファイルに追記を行います。

<?php
  $contents = file_get_contents('./sample_directory/sample_postscript.txt');
  $contents .= "<br />内容を追記しました";
  file_put_contents('./sample_directory/sample_postscript.txt', $contents);

  readfile('./sample_directory/sample_postscript.txt');
?>

XMLを読み込む

下記のようなxmlページがあります。

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" ......>
  <channel>
    <title>xmlサンプル</title>
    <atom:link href="https://test.com/xml/" type="application/rss+xml" />
    <item>
      <title>タイトル1</title>        
    </item>
    <item>
      <title>タイトル2</title>        
    </item>
    <item>
      <title>タイトル3</title>        
    </item>
    <item>
      <title>タイトル4</title>        
    </item>
    <item>
      <title>タイトル5</title>        
    </item>
    <item>
      <title>タイトル6</title>        
    </item>
  </channel>
</rss>

phpでxmlファイルの値を読み込むには以下の関数を使います
simplexml_load_file(xmlファイル名のパス);

各xmlオブジェクトを指定する場合には「->」を使って要素を指定します
同名の要素が複数ある場合にはその要素が配列として格納されているため、番号を指定したり、繰り返し処理を使用して各要素にアクセスします。

<?php
  $xmlObject = simplexml_load_file('https://test.com/xml/');
  foreach ($xmlObject -> channel -> item as $item) :
?><a href="<?php print($item -> link); ?>">
      <?php print($item -> title); ?>
     </a>
<?php
  endforeach;
?>

JSONを読み込む

下記のようなjsonページがあります。

{
  "feed_url": "https://test.com/json",
  "title": "jsonサンプル",
  "description": "jsonデータを格納しています",
  "items": [
    {
      "url": "https://test.com/sample01/",
      "title": "サンプル1"
    },
    {
      "url": "https://test.com/sample02/",
      "title": "サンプル2"
    },
    {
      "url": "https://test.com/sample03/",
      "title": "サンプル3"
    },
    {
      "url": "https://test.com/sample04/",
      "title": "サンプル4"
    }
  ]
}

phpでjsonファイルの値を読み込むには通常のファイルの読み込みの関数を使います
file_get_contents(jsonファイルのパス)
読み込んだファイルをjson形式に変換(デコード)します
オブジェクトへのアクセスの仕方はxmlと同様に「->」を使います。

<?php
  $file = file_get_contents('https://test.com/json');
  $json = json_decode($file);

  foreach ($json -> items as $item) :
?><a href="<?php print($item -> url); ?>"><?php print($item -> title); ?></a>
<?php
  endforeach
?>
0
0
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
0
0