touch 함수

함수 touch는 파일의 마지막 접근 및 수정 시간을 변경할 수 있습니다. 지정된 파일이 존재하지 않으면, touch는 파일을 생성할 수 있습니다(기본적으로). 시간은 명시적으로 지정하거나 현재 시간을 사용할 수 있습니다.

구문

phptouch(string $filename, int $time = null, int $atime = null): bool

예시

현재 시간으로 새 파일 생성:

php<?php
  $file = 'newfile.txt';
  
  if (touch($file)) {
    echo "파일 $file 이(가) 생성됨";
  }
?>

코드 실행 결과:

php"파일 newfile.txt 이(가) 생성됨"

예시

기존 파일의 시간 변경:

php<?php
  $file = 'existing.txt';
  $time = strtotime('2023-01-01 12:00:00');
  if (touch($file, $time)) {
    echo "파일 $file 의 시간이 변경됨";
  }
?>

코드 실행 결과:

php"파일 existing.txt 의 시간이 변경됨"

예시

접근 시간과 수정 시간을 별도로 변경:

php<?php
  $file = 'test.txt';
  $mtime = strtotime('2023-01-01 12:00:00');
  $atime = strtotime('2023-01-02 12:00:00');
  
  if (touch($file, $mtime, $atime)) {
    echo "파일의 타임스탬프가 갱신됨";
  }
?>

함께 보기

  • 함수 filemtime,
    파일의 마지막 수정 시간을 반환합니다.
  • 함수 fileatime,
    파일의 마지막 접근 시간을 반환합니다.
  • 함수 fopen,
    파일이나 URL을 엽니다.