0
0

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 3 years have passed since last update.

file_get_contentsで取得したファイルにfile_put_contentsで記述を追加する方法

Posted at

テキスト追加の例

txt.txt

このファイルはファイルプットコンテンツで書き換えられます
どうぞ
ご確認ください

file_put.php

$file = 'txt.txt';
// ファイルをオープンして既存のコンテンツを取得します
$current = file_get_contents($file);
echo $current;
$current .= "\n"."この行は追加ファイルです。\n";
// 結果をファイルに書き出します
file_put_contents($file, $current);

上記実行後のtxt.txt

このファイルはファイルプットコンテンツで書き換えられます
どうぞ
ご確認ください

この行は追加ファイルです。

テキスト上書きの例(file_put.php)

<?php 
$file = 'txt.txt';
$new_txt .= "新規記述です\n";
// 結果をファイルに書き出します
file_put_contents($file, $new_txt);

実行後のtxt.txt

新規記述です

以上。
応用すれば条件によってファイルの出力や取得を使って色々出来そう。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?