Làm cách nào để xóa một thư mục và nội dung của nó trong PHP?

Hướng dẫn này sẽ cung cấp cho bạn ví dụ về cách xóa thư mục php và các tệp trong thư mục. bạn có thể hiểu một khái niệm về xóa thư mục php và tất cả các tệp. Bài đăng này sẽ cung cấp cho bạn ví dụ đơn giản về xóa thư mục php và tất cả các tệp. Hãy thảo luận về cách xóa thư mục với các tập tin trong php

Đôi khi, chúng ta cần xóa tất cả các tệp và thư mục trong một thư mục bằng mã php. Vì vậy, trong bài viết này, tôi sẽ giúp bạn cách xóa thư mục chứa tất cả nội dung bên trong thư mục đó

Hãy xem cả hai ví dụ sẽ giúp bạn

ví dụ 1

Mã số

$folderName = 'images';

removeFolder($folderName);

function removeFolder($folderName) {

if (is_dir($folderName))

$folderHandle = opendir($folderName);

if (!$folderHandle)

return false;

while($file = readdir($folderHandle)) {

if ($file != "." && $file != "..") {

if (!is_dir($folderName."/".$file))

unlink($folderName."/".$file);

else

removeFolder($folderName.'/'.$file);

}

}

closedir($folderHandle);

rmdir($folderName);

return true;

}

?>

ví dụ 2

Mã số

$folderName = 'images2';

removeFiles($folderName);

function removeFiles($target) {

if(is_dir($target)){

$files = glob( $target . '*', GLOB_MARK );

foreach( $files as $file ){

removeFiles( $file );

}

rmdir( $target );

} elseif(is_file($target)) {

unlink( $target );

}

}

?>

Tôi hy vọng nó có thể giúp bạn

❮ Tham khảo hệ thống tập tin PHP

Ví dụ

Xóa thư mục "hình ảnh"

$path = "hình ảnh";
if(. rmdir($path)) {
  echo ("Không thể xóa $path");
}
?>


Định nghĩa và cách sử dụng

Hàm rmdir() xóa thư mục trống

cú pháp

Giá trị tham số

Tham sốMô tảdirBắt buộc. Chỉ định đường dẫn đến thư mục sẽ bị xóabối cảnhTùy chọn. Chỉ định bối cảnh xử lý tệp. Bối cảnh là một tập hợp các tùy chọn có thể sửa đổi hành vi của luồng

chi tiết kỹ thuật

Giá trị trả về. TRUE khi thành công, FALSE nếu thất bạiPhiên bản PHP. 4. 0+
❮ Tham khảo hệ thống tập tin PHP

Hãy xem những bước cần thực hiện để xóa tệp bằng phương pháp này. Trước hết, bạn nên tạo một danh sách tệp bằng phương thức global(). Bước thứ hai là lặp lại danh sách đó. Sau đó, bạn nên kiểm tra xem đó có phải là tệp hay không. Và cuối cùng, đã đến lúc xóa tệp đã cho bằng phương thức unlink()

Đây là một ví dụ

văn bản

Hoặc bạn có thể chọn áp dụng một đoạn mã ngắn cho phương thức global(). Đầu tiên, cần tạo danh sách tệp bằng phương thức global(). Bước tiếp theo là triển khai phương thức array_filter(). Sau đó, nó sẽ ánh xạ danh sách tới phương thức unlink() với array_map

Đây là cách để làm điều đó

Trong khuôn khổ của phương pháp này, cần phải tạo một danh sách các tệp với DirectoryIterator. Sau đó, lặp lại nó. Sau đó, bạn nên xác thực các tệp trong quá trình kiểm tra xem thư mục có dấu chấm hay không. Cuối cùng, áp dụng tham chiếu phương thức getPathName, xóa tệp bằng unlink()

Đây là một ví dụ

Trong đoạn mã này, chúng tôi đã chỉ cho bạn những cách phổ biến và hiệu quả nhất để xóa tệp khỏi thư mục bằng PHP

Xóa tất cả các tệp khỏi một thư mục bằng PHP

Cải thiện bài viết

Lưu bài viết

Thích bài viết

  • Cập nhật lần cuối. 08 tháng 1 năm 2019

  • Đọc
  • Bàn luận
  • khóa học
  • Luyện tập
  • Băng hình
  • Cải thiện bài viết

    Lưu bài viết

    Trong PHP, các tệp từ một thư mục có thể bị xóa bằng nhiều cách tiếp cận khác nhau và các phương thức sẵn có như hủy liên kết, DirectoryIterator và DirectoryRecursiveIterator.
    Một số cách tiếp cận này được giải thích bên dưới.

    Cách tiếp cận 1

    • Tạo danh sách các tệp bằng phương thức glob()
    • Lặp lại danh sách các tập tin
    • Kiểm tra xem tên tệp có hợp lệ không
    • Xóa tệp bằng phương thức unlink()

    Ví dụ




    // PHP program to delete all

    // file from a folder

      

    // Folder path to be flushed

    $folder_path =

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    0____11

      

    1

    2

    3 =5______36$folder_path89// PHP program to delete all0

      

    // PHP program to delete all2

    // PHP program to delete all363 // PHP program to delete all6 // PHP program to delete all7// PHP program to delete all8

      

    // file from a folder0______516// file from a folder36// PHP program to delete all7// file from a folder6

    // file from a folder7

    // file from a folder8// file from a folder9

    // file from a folder8  1// PHP program to delete all7// PHP program to delete all0

      4

      5

    Đầu ra.
    Trước khi chạy mã.

    Làm cách nào để xóa một thư mục và nội dung của nó trong PHP?

    Sau khi chạy mã.
    Làm cách nào để xóa một thư mục và nội dung của nó trong PHP?

    Lưu ý. Các tệp ẩn có thể được bao gồm trong thao tác xóa tệp bằng cách thêm mã bên dưới.




      6 =56$folder_path8// Folder path to be flushed2// Folder path to be flushed3

    Cách tiếp cận 2

    • Tạo danh sách các tệp bằng phương thức glob()
    • Lọc danh sách bằng phương thức array_filter() hoặc array_merge()
    • Ánh xạ danh sách tới phương thức unlink() sử dụng phương thức array_map()

    Ví dụ




    // Folder path to be flushed5

    // Folder path to be flushed6

    // Folder path to be flushed7

    // Folder path to be flushed86$folder_path0$folder_path1$folder_path26

    $folder_path46$folder_path6$folder_path7$folder_path8656=2=3

    // Folder path to be flushed6

      5

    Cách tiếp cận 3

    • Tạo danh sách tệp bằng DirectoryIterator
    • Lặp lại danh sách các tập tin
    • Xác thực tệp trong khi kiểm tra xem thư mục tệp có dấu chấm hay không
    • Sử dụng tham chiếu phương thức getPathName, xóa tệp bằng phương thức unlink()

    Ví dụ




    =7

    =8

    // Folder path to be flushed6

    // Folder path to be flushed

    $folder_path =

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    03____11

    // Folder path to be flushed6

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    06

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    07 =

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    09

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    10$folder_path

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    12

    // Folder path to be flushed6

    // PHP program to delete all2

    // PHP program to delete all3 6

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    07 // PHP program to delete all6

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    19// PHP program to delete all8

    01

    // file from a folder0// file from a folder1 04

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    1906

    // Folder path to be flushed6

    // file from a folder8// file from a folder9

    _______58____61____119____313

    // file from a folder0  4

      4

      5

    Cách tiếp cận 4

    • Tạo danh sách tất cả các hệ thống phân cấp thư mục bên trong thư mục thư mục đã cho bằng cách sử dụng RecursiveDirectoryIterator
      $dir = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
      $dir = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);
    • Hệ thống tập tinIterator. SKIP_DOTS được sử dụng để bỏ qua các dấu chấm trong khi tạo danh sách tệp
    • Đệ quyIteratorIterator. CHILD_FIRST được sử dụng để chọn các tệp có trong thư mục gốc
    • Lặp lại danh sách tệp và xóa các thư mục và tệp theo thông số kỹ thuật

    Ví dụ




    19

    =8

      

    // Folder path to be flushed

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    07 =____325____11

      

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    06

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    07 =

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    09 32

    _______50____107____335

      

    37

    38

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    07 =

    $folderName = 'images2';

    removeFiles($folderName);

    function removeFiles($target) {

    if(is_dir($target)){

    $files = glob( $target . '*', GLOB_MARK );

    foreach( $files as $file ){

    removeFiles( $file );

    }

    rmdir( $target );

    } elseif(is_file($target)) {

    unlink( $target );

    }

    }

    ?>

    09 42

    _______50____107____345

      

    47

    48

    // PHP program to delete all3 6_______107 // PHP program to delete all6 // PHP program to delete all7 54

    // file from a folder0______4757586// PHP program to delete all761// PHP program to delete all763

      4

      5


    Ghi chú cá nhân của tôi arrow_drop_up

    Tiết kiệm

    Vui lòng Đăng nhập để nhận xét.

    Làm cách nào để xóa nội dung của một thư mục trong PHP?

    Cách tiếp cận 1. .
    Tạo danh sách các tệp bằng phương thức glob()
    Lặp lại danh sách các tập tin
    Kiểm tra xem tên tệp có hợp lệ không
    Xóa tệp bằng phương thức unlink()

    Làm cách nào để tạo và xóa một thư mục trong PHP?

    PHP tạo & xóa thư mục . Mkdir(*/temp/sample*, 0777); Đang xóa thư mục. rmdir() của PHP có thể được sử dụng để xóa các thư mục.

    Làm cách nào để xóa tất cả hình ảnh trong một thư mục trong PHP?

    Bạn có thể xóa tệp trong PHP bằng hàm unlink() .