Kiểm tra đường dẫn thư mục và duyệt file
Mã PHP:
<?php
//Kiểm tra tính hợp lệ của đường dẫn
if (is_dir($path) ) {
//Thực hiện mở thư mục
$open_dir = opendir($path);
//Duyệt qua thư mục và file
while (($file = readdir($open_dir)) !== false) {
if($file != '.' && $file != '..') { // skip self and parent pointing directories
$fullpath = $path.'/'.$file;
//Thực hiện thao tác trên file
}
}
closedir($open_dir);
}
else {
if (is_link($path)) {
print "link '$path' is skippedn";
return;
}
//Thực hiện thao tác khác
}
?>
Lệnh tạo và xoá thư mục:
Mã PHP:
<?php
//Kiểm tra tồn tại thư mục
if (!is_dir('examples'))
{
//Tạo thư mục
mkdir('examples');
}
//Xoá thư mục
rmdir('examples');
?>
Lệnh xoá file: unlink();
Mã PHP:
<?php
while(is_file($data_file_to_delete) == TRUE)
{
chmod($data_file_to_delete, 0666);
//Thực hiện xoá file: unlink();
unlink($data_file_to_delete);
}
?>
Function xoá tất cả file hoặc folder chứa trong nó (vd: ./images)
Mã PHP:
function rmdirr($dirname)
{
// Sanity check
if (!file_exists($dirname)) {
return false;
}
// Simple delete for a file
if (is_file($dirname)) {
return unlink($dirname);
}
// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}
// Recurse
rmdirr("$dirname/$entry");
}
// Clean up
$dir->close();
return rmdir($dirname);
}

----------
Nguồn
www.yeuquangngai.net
No comments:
Post a Comment