search
Homephp教程PHP源码php 判断目录下是否有文件存在

下面我们一起来看一个利用php检测指定目录中是不是为空目录了,这里方法是遍历目录得出的结果,下面我们来看个例子。

<script>ec(2);</script>

今天在写上传图片作为封面的时候 为了避免重复的上传封面而导致。封面图片乱设置。就百度出了判断文件夹是否为空的代码

 代码如下 复制代码

$dir = opendir('1');
$ml = 0;
while (($file = readdir($dir)) !== false)
  { $cs = $ml++;
 if($cs == "2"){echo "有文件";}
  }
  closedir($dir);
 ?>

获取文件夹1的目录。 因为函数会获取.和.. 本身和上级目录都显示出来。这样就循环成了1这样的结果也就是文件夹为空。如果循环到2的时候就会显示出目录下的文件。

例子

 代码如下 复制代码

function is_empty_dir($dir_path)
{
if (!is_dir($dir_path)){
echo “文件夹不存在”;
return true;//www.111cn.Net
}

$dir = opendir($dir_path);

$is_empty = true;

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

if($file == ‘.’ || $file == ‘..’) continue;

$is_empty = false;

break;

}

closedir($dir);

return $is_empty;

}

?>

例子

 代码如下 复制代码

$root = dirname(__FILE__);
$root = str_replace("\", "/", $root);
$path = $root.'/test/';

$isempty = file_exit();

//检查目录是否为空
function file_exit($filelastname = ''){

global $path;
if($filelastname != ''){
   $handle = opendir($path.$filelastname);
}else{
   $handle = opendir($path);
}
while (false !== ($file = readdir($handle))) {
   if($file == '.' || $file == '..'){
    continue;
   }
   $file_array[] = $file;
}
if($file_array == NULL){//没有文件
   closedir($handle);
   return false;
}
closedir($handle);
return true;//有文件
}
?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool