PHP Delete all files that are empty in a Directory and any sub directories

compcobalt

This is what I have but its not working nor giving me an errors:

$MyDir = "C:/some_folder/"; 

// DELETE ALL EMPTY FILES
$filesDVA = glob($MyDir.'*'); // get all file names
foreach($filesDVA as $file){ // iterate files
  if(empty($file))
    unlink($file); // delete file
}

I would like to delete the empty files that are in the main directory and sub directories and if possible check if the directory is empty also and if it is delete it too.

UPDATE:

foreach (glob($MyDir . '*') as $file) {
    if (is_writable($file) && filesize($file) < (1024 * 1)) {
        unlink($file);
    }
}

It removes the empty files (or files that are less than 1kb but it gives me an error when trying to access the directories saying Permission denied for each directory, so it will NOT delete the empty directories or the files that are within the sub directories.

Alex Howansky

empty($file) doesn't check that the file referenced by $file is empty, it checks that the variable $file is empty. I.e., if $file contains an empty string "" or null then empty($file) will return true. Since your $file contains a non-empty string (the name of the file), empty($file) will always return false, regardless of the file contents. You want to check that filesize($file) is zero.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to delete all files in a directory, keeping sub-directories intact

From Dev

Delete all files and sub directories but keep main directory

From Dev

recursively delete all files, empty directories, and directories with files of multiple names under current directory, including current directory

From Dev

How can i add a recursive loop to delete a directory with all sub directories and files inside?

From Dev

Delete files within all directories in a directory

From Dev

How can I recursively find a directory by name and delete its contents (including all sub-directories and files) while keeping the directory itself?

From Dev

How can I recursively find a directory by name and delete its contents (including all sub-directories and files) while keeping the directory itself?

From Dev

How to list directories, sub-directories and all files in php

From Dev

Python 2.7 How to list files in a directory AND all sub directories?

From Dev

Change group access to directory and all sub directories and files

From Dev

Change group access to directory and all sub directories and files

From Dev

list all c files in sub directories and their parent directory

From Dev

zip files within all sub-directories of a given parent directory

From Dev

Listing all files in a directory and sub-directories C#

From Dev

How to delete all files in sub directories in 1 hit permanently

From Dev

R: efficiently delete all empty files in a directory

From Dev

R: efficiently delete all empty files in a directory

From Dev

PHP : list files in directory and sub-directories into an XML file

From Dev

How can I recursively delete all empty files and directories in Linux?

From Dev

How to delete all directories inside a directory except for files, (safely)?

From Dev

delete all empty directories starting with

From Dev

How to efficiently delete empty directories (and empty sub-directories)

From Dev

Rewrite any sub-domain and all files to a single directory

From Java

How to download HTTP directory with all files and sub-directories as they appear on the online files/folders list?

From Dev

Git: ignore files but not sub-directories in a directory

From Dev

assigning files in a directory to sub-directories

From Dev

How to split a directory of files into sub-directories

From Dev

find matching files in one directory and sub directories

From Dev

Rename multiple files in sub directories to their directory names

Related Related

  1. 1

    How to delete all files in a directory, keeping sub-directories intact

  2. 2

    Delete all files and sub directories but keep main directory

  3. 3

    recursively delete all files, empty directories, and directories with files of multiple names under current directory, including current directory

  4. 4

    How can i add a recursive loop to delete a directory with all sub directories and files inside?

  5. 5

    Delete files within all directories in a directory

  6. 6

    How can I recursively find a directory by name and delete its contents (including all sub-directories and files) while keeping the directory itself?

  7. 7

    How can I recursively find a directory by name and delete its contents (including all sub-directories and files) while keeping the directory itself?

  8. 8

    How to list directories, sub-directories and all files in php

  9. 9

    Python 2.7 How to list files in a directory AND all sub directories?

  10. 10

    Change group access to directory and all sub directories and files

  11. 11

    Change group access to directory and all sub directories and files

  12. 12

    list all c files in sub directories and their parent directory

  13. 13

    zip files within all sub-directories of a given parent directory

  14. 14

    Listing all files in a directory and sub-directories C#

  15. 15

    How to delete all files in sub directories in 1 hit permanently

  16. 16

    R: efficiently delete all empty files in a directory

  17. 17

    R: efficiently delete all empty files in a directory

  18. 18

    PHP : list files in directory and sub-directories into an XML file

  19. 19

    How can I recursively delete all empty files and directories in Linux?

  20. 20

    How to delete all directories inside a directory except for files, (safely)?

  21. 21

    delete all empty directories starting with

  22. 22

    How to efficiently delete empty directories (and empty sub-directories)

  23. 23

    Rewrite any sub-domain and all files to a single directory

  24. 24

    How to download HTTP directory with all files and sub-directories as they appear on the online files/folders list?

  25. 25

    Git: ignore files but not sub-directories in a directory

  26. 26

    assigning files in a directory to sub-directories

  27. 27

    How to split a directory of files into sub-directories

  28. 28

    find matching files in one directory and sub directories

  29. 29

    Rename multiple files in sub directories to their directory names

HotTag

Archive