Home > Backend Development > PHP Tutorial > How to get the first-level directory of a file in php (pure code)

How to get the first-level directory of a file in php (pure code)

不言
Release: 2023-04-03 15:52:01
Original
3779 people have browsed it

The content of this article is about how to obtain the first-level directory of files (pure code) in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

php obtains the first-level example code of the file directory (solve the problem of Chinese garbled characters);

// 获取目录一层的方法
/*
   *  @param  readfileone //读取一层文件夹内容
   *  @param  dir  // 查找的路径
   *  @param
   *  @param
   */
function   readfileone($dirName){
    $array=array();
    $dir='./Public/Uploads/'.$dirName;
    if(is_dir(iconv('utf-8','gb2312',$dir))==true){
        if($handle=opendir(iconv('utf-8','gb2312',$dir))){
            while(false!==($files=readdir($handle))){
                $files=iconv('gb2312','utf-8',$files);
                if($files!="."&&$files!=".."){
                    $files=iconv('utf-8','utf-8',$files);
                    $dir=iconv('utf-8','utf-8',$dir);
                    if(is_dir($dir."/".$files))
                    {
                        $new_dir=$dir."/".$files;
                        readfileone($new_dir);
                    }
                    else
                    {
                        $str=$dir."/".$files;
                        $str=iconv('utf-8','utf-8',$str);
                        // session('success.file',$str);
                        array_push($array,$str);
                    }
                }
            }
        }
        closeDir($handle);
    }else{
        //  echo '没有找到目录';
        array_push($array,'没有找到目录');
        array_push($array,'fail');
    }
    return $array;//返回二维数组
}
Copy after login

Recommended related articles:

How to create php File Directory? (Pure code)

Code example of how PHP deletes a directory and all files in the directory

How PHP decompresses the compressed package file to a specified location Directory? (Pure code)

The above is the detailed content of How to get the first-level directory of a file in php (pure code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template