php
PHP - touch 함수
bool touch ( string $filename [, int $time= time() [, int $atime ]] )
(PHP 4, PHP 5)
이 함수는 파일 액세스, 수정시간을 강제로 수정하며, time 이나 atime 인자를 사용하지 않을 경우 현재 시간으로 설정합니다.
참고로 filectime 함수는 파일 변경시간을, filemtime 함수는 수정시간을 정수로 가져옵니다.
물론 액세스한 시간이나 기타 정보를 가져 올 수 있는 stat 함수도 있습니다. 이 함수는 다음 표의 정보를 배열로 가져옵니다.
(PHP 4, PHP 5)
이 함수는 파일 액세스, 수정시간을 강제로 수정하며, time 이나 atime 인자를 사용하지 않을 경우 현재 시간으로 설정합니다.
<?php
// 액세스한 시간을 한 시간 전으로 되돌립니다.
$time = time() - 3600;
if(!touch('test.php', $time)) {
echo '수정 실패...';
}else{
echo '수정 완료...';
}
?>
<?php
if (touch('test.php')) {
echo '수정 완료';
} else {
echo '수정 실패';
}
?>
<?php
// 1초를 지연합니다.
touch('test.php');
echo filemtime('index.php')."<br />\n"; // 결과: 1309248181
clearstatcache();
sleep(1);
// 1초를 지연합니다.
touch('test.php');
echo filemtime('index.php')."<br />\n"; // 결과: 1309248182
clearstatcache();
sleep(1);
// 3초를 지연합니다.
touch('test.php');
echo filemtime('index.php')."<br />\n"; // 결과: 1309248183
clearstatcache();
sleep(3);
// 2초를 지연합니다.
touch('test.php');
echo filemtime('index.php')."<br />\n"; // 결과: 1309248186
clearstatcache();
sleep(2);
touch('test.php');
echo filemtime('index.php')."<br />\n"; // 결과: 1309248188
clearstatcache();
?>
참고로 filectime 함수는 파일 변경시간을, filemtime 함수는 수정시간을 정수로 가져옵니다.
<?php
echo filectime ('test.php')."<br />\n"; // 결과: 1309249648
clearstatcache();
sleep(1);
echo filemtime ('test.php')."<br />\n"; // 결과: 1309249648
clearstatcache();
sleep(1);
// 파일을 수정합니다.
$fp = fopen('test.php', 'a');
if(is_resource($fp)){
fwrite($fp, 'test');
fclose($fp);
}
// 강제 변경합니다.
touch('test.php');
echo filemtime ('test.php')."<br />\n"; // 결과: 1309249683
clearstatcache();
sleep(1);
echo filemtime ('test.php')."<br />\n"; // 결과: 1309249683
clearstatcache();
sleep(1);
echo filectime ('test.php')."<br />\n"; // 결과: 1309249683
clearstatcache();
sleep(1);
?>
물론 액세스한 시간이나 기타 정보를 가져 올 수 있는 stat 함수도 있습니다. 이 함수는 다음 표의 정보를 배열로 가져옵니다.
배열 키 | 설 명 |
---|---|
dev | device number. 함수명: fstat() |
ino | inode number. 함수명: lstat(), fstat() |
mode | inode 보호 모드. 함수명: lstat(), fstat() |
nlink | 링크 번호. 함수명: fstat() |
uid | 소유자 사용자 id. 함수명: fstat() |
gid | 소유자 그룹 id. 함수명: fstat() |
rdev | 디바이스 타입 |
size | 파일 크기. 함수명: lstat(), fstat() |
atime | 엑세스 시간, 타임스탬프. 함수명: lstat(), fstat() |
mtime | 수정 시간, 타임스탬프. 함수명: lstat(), fstat() |
ctime | 변경 시간, 타임스탬프. 함수명: lstat(), fstat() |
blksize | 최적의 블럭 크기. 함수명: fstat() |
blocks | 할당된 블럭의 수. 함수명: lstat(), fstat() |
<?php
$info = stat('test.php');
print_r($info);
/*
결과:
Array
(
[0] => 2066
...
[dev] => 2066
[ino] => 203029047
[mode] => 33279
[nlink] => 1
[uid] => 2696
[gid] => 500
[rdev] => 0
[size] => 2661
[atime] => 1309249269
[mtime] => 1309248657
[ctime] => 1309248657
[blksize] => 4096
[blocks] => 8
)
*/
?>
<?php
function ustat($file) {
clearstatcache();
$ss=@stat($file);
if(!$ss) {
return false;
}
$ts=array(
0140000=>'ssocket',
0120000=>'llink',
0100000=>'-file',
0060000=>'bblock',
0040000=>'ddir',
0020000=>'cchar',
0010000=>'pfifo'
);
$p = $ss['mode'];
$t = decoct($ss['mode'] & 0170000);
$str=(array_key_exists(octdec($t),$ts))?$ts[octdec($t)]{0}:'u';
$str.=(($p&0x0100)?'r':'-').(($p&0x0080)?'w':'-');
$str.=(($p&0x0040)?(($p&0x0800)?'s':'x'):(($p&0x0800)?'S':'-'));
$str.=(($p&0x0020)?'r':'-').(($p&0x0010)?'w':'-');
$str.=(($p&0x0008)?(($p&0x0400)?'s':'x'):(($p&0x0400)?'S':'-'));
$str.=(($p&0x0004)?'r':'-').(($p&0x0002)?'w':'-');
$str.=(($p&0x0001)?(($p&0x0200)?'t':'x'):(($p&0x0200)?'T':'-'));
if(function_exists('posix_getpwuid')){
$oid = @posix_getpwuid($ss['uid']);
} else {
$oid = false;
}
if(function_exists('posix_getgrgid')){
$gid = @posix_getgrgid($ss['gid']);
} else {
$gid = false;
}
$info = array();
$info = array(
'perms'=>array(
'umask'=>sprintf("%04o",@umask()),
'human'=>$str,
'octal1'=>sprintf("%o", ($ss['mode'] & 000777)),
'octal2'=>sprintf("0%o", 0777 & $p),
'decimal'=>sprintf("%04o", $p),
'fileperms'=>@fileperms($file),
'mode1'=>$p,
'mode2'=>$ss['mode']),
'owner'=>array(
'fileowner'=>$ss['uid'],
'filegroup'=>$ss['gid'],
'owner'=> $oid,
'group'=> $gid
),
'file'=>array(
'filename'=>$file,
'realpath'=>(@realpath($file) != $file) ? @realpath($file) : '',
'dirname'=>@dirname($file),
'basename'=>@basename($file)
),
'filetype'=>array(
'type'=>substr($ts[octdec($t)],1),
'type_octal'=>sprintf("%07o", octdec($t)),
'is_file'=>@is_file($file),
'is_dir'=>@is_dir($file),
'is_link'=>@is_link($file),
'is_readable'=> @is_readable($file),
'is_writable'=> @is_writable($file)
),
'device'=>array(
'device'=>$ss['dev'], //Device
'device_number'=>$ss['rdev'], //Device number, if device.
'inode'=>$ss['ino'], //File serial number
'link_count'=>$ss['nlink'], //link count
'link_to'=>($s['type']=='link') ? @readlink($file) : ''
),
'size'=>array(
'size'=>$ss['size'], //Size of file, in bytes.
'blocks'=>$ss['blocks'], //Number 512-byte blocks allocated
'block_size'=> $ss['blksize'] //Optimal block size for I/O.
),
'time'=>array(
'mtime'=>$ss['mtime'],
'atime'=>$ss['atime'],
'ctime'=>$ss['ctime'],
'accessed'=>@date('Y M D H:i:s',$ss['atime']),
'modified'=>@date('Y M D H:i:s',$ss['mtime']),
'created'=>@date('Y M D H:i:s',$ss['ctime'])
),
);
clearstatcache();
return $info;
}
$info = ustat("test.php");
print_r($info);
/*
결과:
Array
(
[perms] => Array
(
[umask] => 0022
[human] => -rw-r--r--
[octal1] => 644
[octal2] => 0644
[decimal] => 100644
[fileperms] => 33188
[mode1] => 33188
[mode2] => 33188
)
[owner] => Array
(
[fileowner] => 2696
[filegroup] => 500
[owner] => Array
(
[name] => root
[passwd] => x
[uid] => 2696
[gid] => 500
[gecos] =>
[dir] => /Server/html
[shell] => /bin/bash
)
[group] => Array
(
[name] => host
[passwd] => x
[members] => Array
(
)
[gid] => 500
)
)
[file] => Array
(
[filename] => test.php
[realpath] => /Server/html/test.php
[dirname] => .
[basename] => test.php
)
[filetype] => Array
(
[type] => file
[type_octal] => 0100000
[is_file] => 1
[is_dir] =>
[is_link] =>
[is_readable] => 1
[is_writable] =>
)
[device] => Array
(
[device] => 2066
[device_number] => 0
[inode] => 201882963
[link_count] => 1
[link_to] =>
)
[size] => Array
(
[size] => 2720
[blocks] => 8
[block_size] => 4096
)
[time] => Array
(
[mtime] => 1309252725
[atime] => 1309252730
[ctime] => 1309252725
[accessed] => 2011 Jun Tue 18:18:50
[modified] => 2011 Jun Tue 18:18:45
[created] => 2011 Jun Tue 18:18:45
)
)
*/
?>
0 댓글