CuDemVN.Wap.Sh



![]() | ![]() ![]() ![]() |
Bạn nào muốn sử dụng chế độ tự động thì sử dụng chức năng cron job trong host nhá, hoặc bạn cũng có thể sử dụng
free cronjobs
- Thích hợp làm Wap lưu trữ theo phiên hoặc làm ảnh chẳng hạn.
Trong quá trình làm việc tôi thấy cần thiết xóa 1 folder trong 1 thư mục nào đó. Tôi phải phải làm 2 bước.
Bước 1: Xóa tất cả các file chứa trong folder này
Bước 2: Xóa folder này (Khi folder này rỗng)
Đoạn code PHP như sau:
<?php
function remove_directory($dir) {
if ($handle=opendir("$dir")) {
while (false!== ($item=readdir($handle))) {
if ($item!="."&&$item!="..") {
if (is_dir("$dir/$item")) {
remove_directory("$dir/$item");
} else {
unlink("$dir/$item");
echo "removing $dir/$item<br>\n";
}
}
}
closedir($handle);
rmdir($dir);
echo "removing $dir<br>\n";
}
}
?>
Cách dùng:
<?php
remove_directory("đường dẫn tới thư mục cần xoá");
?>
remove_directory("đường dẫn tới thư mục cần xoá");
?>
→ Nếu bạn muốn xóa tất cả các file chứa trong folder này thôi thì dùng code như thế này
<?php
function remove_allFile($dir) {
if ($handle=opendir("$dir")) {
while (false!== ($item=readdir($handle))) {
if ($item!="."&&$item!="..") {
if (is_dir("$dir/$item")) {
remove_directory("$dir/$item");
} else {
unlink("$dir/$item");
echo "removing item $dir/$item\n";
}
}
}
closedir($handle);
}
}
?>
Cách dùng:
<?php
remove_allFile("đường dẫn tới thư mục cần xoá hết file");
?>
remove_allFile("đường dẫn tới thư mục cần xoá hết file");
?>
Trên đây là hàm PHP xoá thư mục và file trên hosting, rất quan trọng và hữu ích cho các dự án của bạn. Hãy lưu lại để tham khảo nhé!

Nguồn: SinhThanhZ
↓ Tập tin Đính kèm ↓

Đánh giá: 4.5/ 5, 1850 bình chọn
