PHP 8.4.1 Released!

目录函数函数

参见

相关函数,例如:dirname()is_dir()mkdir()rmdir(), 请查看文件系统章节。

目录

  • chdir — 改变目录
  • chroot — 改变根目录
  • closedir — 关闭目录句柄
  • dir — 返回一个 Directory 类实例
  • getcwd — 取得当前工作目录
  • opendir — 打开目录句柄
  • readdir — 从目录句柄中读取条目
  • rewinddir — 倒回目录句柄
  • scandir — 列出指定路径中的文件和目录
添加备注

用户贡献的备注 1 note

up
12
dkflbk at nm dot ru
18 years ago
I wrote a simple backup script which puts all files in his folder (and all of the sub-folders) in one TAR archive...
(It's classic TAR format not USTAR, so filename and path to it can't be longer then 99 chars)
<?php
/***********************************************************
* Title: Classic-TAR based backup script v0.0.1-dev
**********************************************************/

Class Tar_by_Vladson {
var
$tar_file;
var
$fp;
function
Tar_by_Vladson($tar_file='backup.tar') {
$this->tar_file = $tar_file;
$this->fp = fopen($this->tar_file, "wb");
$tree = $this->build_tree();
$this->process_tree($tree);
fputs($this->fp, pack("a512", ""));
fclose($this->fp);
}
function
build_tree($dir='.'){
$handle = opendir($dir);
while(
false !== ($readdir = readdir($handle))){
if(
$readdir != '.' && $readdir != '..'){
$path = $dir.'/'.$readdir;
if (
is_file($path)) {
$output[] = substr($path, 2, strlen($path));
} elseif (
is_dir($path)) {
$output[] = substr($path, 2, strlen($path)).'/';
$output = array_merge($output, $this->build_tree($path));
}
}
}
closedir($handle);
return
$output;
}
function
process_tree($tree) {
foreach(
$tree as $pathfile ) {
if (
substr($pathfile, -1, 1) == '/') {
fputs($this->fp, $this->build_header($pathfile));
} elseif (
$pathfile != $this->tar_file) {
$filesize = filesize($pathfile);
$block_len = 512*ceil($filesize/512)-$filesize;
fputs($this->fp, $this->build_header($pathfile));
fputs($this->fp, file_get_contents($pathfile));
fputs($this->fp, pack("a".$block_len, ""));
}
}
return
true;
}
function
build_header($pathfile) {
if (
strlen($pathfile) > 99 ) die('Error');
$info = stat($pathfile);
if (
is_dir($pathfile) ) $info[7] = 0;
$header = pack("a100a8a8a8a12A12a8a1a100a255",
$pathfile,
sprintf("%6s ", decoct($info[2])),
sprintf("%6s ", decoct($info[4])),
sprintf("%6s ", decoct($info[5])),
sprintf("%11s ",decoct($info[7])),
sprintf("%11s", decoct($info[9])),
sprintf("%8s", " "),
(
is_dir($pathfile) ? "5" : "0"),
"",
""
);
clearstatcache();
$checksum = 0;
for (
$i=0; $i<512; $i++) {
$checksum += ord(substr($header,$i,1));
}
$checksum_data = pack(
"a8", sprintf("%6s ", decoct($checksum))
);
for (
$i=0, $j=148; $i<7; $i++, $j++)
$header[$j] = $checksum_data[$i];
return
$header;
}
}

header('Content-type: text/plain');
$start_time = array_sum(explode(chr(32), microtime()));
$tar = & new Tar_by_Vladson();
$finish_time = array_sum(explode(chr(32), microtime()));
printf("The time taken: %f seconds", ($finish_time - $start_time));
?>
To Top