TOP ▲ itcore TOP プログラムパーツ

uUnlink ファイルが存在すれば削除する。 | itcore 2019年

PHP

PHP TOP

関数

<?php
function uUnlink($file) {
  if (file_exists($file)) unlink($file);
}
?>

テストプログラム

<?php include_once "uUnlink.func"; ?>
<?php
$file = "/tmp/unlink.tmp";
$data ="test";
file_put_contents($file, $data);
echo "1 ".file_get_contents3($file)."<br>\n";
uUnlink($file);
echo "2 ".file_get_contents3($file)."<br>\n";
uUnlink($file);

//-------------------------------------
// ファイルが無ければエラーを出す。
function file_get_contents3($file) {
  if (file_exists($file)) return file_get_contents($file);
  echo "ファイルがありません。($file)<br>\n";
  return "";
}
?>

実行結果

1 test
ファイルがありません。(/tmp/unlink.tmp)
2