戻る

ファイルの更新日時を強制的に変更する(参考)

touch()関数

touch()関数を使うと、ファイルの最終更新日時を任意の日時に強制的に変更できます。この関数では、第1引数に「対象ファイル名」、第2引数に「変更したい日時」をタイムスタンプの形式で指定します。この例ではmktime()関数で具体的な日時「2006年1月1日0時0分0秒」を指定して変更する例と、「strtotime("now")」によってその時点の現在日時に変更する例を示しています。

PHP

<?php
header("Content-type: text/html; charset=utf-8");

$file = "images/gihyo.gif";

echo "変更前の最終更新日時 → " . date("Y/m/d H:i:s", filemtime($file)) . "<br><br>";

touch($file, mktime(0, 0, 0, 1, 1, 2006));

clearstatcache();

echo "変更後の最終更新日時 → " . date("Y/m/d H:i:s", filemtime($file)) . "<br><br>";

touch($file, strtotime("now"));

clearstatcache();

echo "変更後の最終更新日時 → " . date("Y/m/d H:i:s", filemtime($file)) . "<br><br>";
?>

inserted by FC2 system