How to Use PHP to Check If a Directory Is Empty

How can I use PHP to check if a directory is empty?

It seems that you need scandir instead of glob, as glob can't see unix hidden files.

<?php
$pid = basename($_GET["prodref"]); //let's sanitize it a bit
$dir = "/assets/$pid/v";

if (is_dir_empty($dir)) {
echo "the folder is empty";
}else{
echo "the folder is NOT empty";
}

function is_dir_empty($dir) {
if (!is_readable($dir)) return null;
return (count(scandir($dir)) == 2);
}
?>

Note that this code is not the summit of efficiency, as it's unnecessary to read all the files only to tell if directory is empty. So, the better version would be

function dir_is_empty($dir) {
$handle = opendir($dir);
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
closedir($handle);
return false;
}
}
closedir($handle);
return true;
}

By the way, do not use words to substitute boolean values. The very purpose of the latter is to tell you if something empty or not. An

a === b

expression already returns Empty or Non Empty in terms of programming language, false or true respectively - so, you can use the very result in control structures like IF() without any intermediate values

PHP: The fastest way to check if the directory is empty?

The opendir()/readdir() and FilesystemIterator approaches are both conceptually equivalent, and perform identical system calls (as tested on PHP 7.2 running under Linux). There's no fundamental reason why either one would be faster than the other, so I would recommend that you run benchmarks if you need to microoptimize.

The approach using glob() will perform worse. glob() returns an array of all the filenames in the directory; constructing that array can take some time. If there are many files in the directory, it will perform much worse, as it must iterate through the entire contents of the directory.

Using glob() will also give incorrect results in a number of situations:

  • If $dir is a directory name which contains certain special characters, including *, ?, and [/]
  • If $dir contains only dotfiles (i.e, filenames starting with .)

As for the Answer 1, please note that I added closedir($dir);, but I'm not sure if that's necessary (?).

It's a good idea, but you've implemented it incorrectly. The directory handle that needs to be closed is $handle, not $dir.

PHP: What is the best and easiest way to check if directory is empty or not

Use glob :

if (count(glob("path/*")) === 0 ) { // empty

A nice thing about glob is that it doesn't return . and .. directories.

Check if folder is empty or not php

This should work for you:

(Here I just use glob() to get all files of the directory and check if it is empty or not. Your method didn't worked, because scandir() also includes . and ..)

public function fileExist()
{
$open = "../asset/files";

if ($files = glob($open . "/*")) {
echo "You got files";
print_r($files);
} else {
echo "You haven't got files";
}
}

Unable to check if folder is empty

I think the way you are constructing the path wont work. Please try with this:

<?php
$userid = '000019';

$dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "Trips" . DIRECTORY_SEPARATOR . $userid . DIRECTORY_SEPARATOR;

function dir_is_empty($path, $hiddenFilesCountAsFiles = true)
{
$empty = true;
if (!is_dir($path)) throw new Exception("$path is not a valid path");
if (!is_readable($path)) throw new Exception("$path is not readable");

if ($dir = opendir($path)) {
while(false !== ($file = readdir($dir))) {
if($hiddenFilesCountAsFiles && strpos($file, ".") !== 0) {
$empty = false;
break;
} elseif (!$hiddenFilesCountAsFiles && $file != "." && $file != "..") {
$empty = false;
break;
}
}
closedir($dir);
} else {
throw new Exception("Could not open $path");
}
return $empty;
}

$empty = (dir_is_empty($dir)) ? "true" : "false";
echo "Path $dir is empty: $empty";

if ($empty === "false") {
echo "Directory contents:<br>";
if ($dir = opendir($path)) {
while(false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
echo "$file<br>";
}
}
closedir($dir);
} else {
echo "Could not open directory";
}

}

Best way to check if a dir is empty whith php

Don't think so. Shortest/quickest way I can think of is the following, which should work as far as I can see.

function dir_is_empty($path)
{
$empty = true;
$dir = opendir($path);
while($file = readdir($dir))
{
if($file != '.' && $file != '..')
{
$empty = false;
break;
}
}
closedir($dir);
return $empty;
}

This should only go through a maximum of 3 files. The two . and .. and potentially whatever comes next. If something comes next, it's not empty, and if not, well then it's empty.

How to check,that there's nothing in the folder?

You can count the items in the array

if (count($files) > 2)

How to check if folder has at least 1 file in php

You can achieve it by using opendir and readdir

function check_file($dir) {
$result = false;
if($dh = opendir($dir)) {
while(!$result && ($file = readdir($dh)) !== false) {
$result = $file !== "." && $file !== ".." && !is_dir($file);
}

closedir($dh);
}

return $result;
}

$dir="/var/www/html/jkjkj/";// your path here
echo check_file($dir);// return 1 if file found otherwise empty


Related Topics



Leave a reply



Submit