<menu id="ycqsw"></menu><nav id="ycqsw"><code id="ycqsw"></code></nav>
<dd id="ycqsw"><menu id="ycqsw"></menu></dd>
  • <nav id="ycqsw"></nav>
    <menu id="ycqsw"><strong id="ycqsw"></strong></menu>
    <xmp id="ycqsw"><nav id="ycqsw"></nav>
  • php獲取目錄下的文件名(php顯示文件目錄及路徑)


    直接上代碼,復制即用

        protected $paths=[];
        /**
         * @description: 獲取文件路徑
         * @param {type} 
         * @return: 
         */
        protected function getDir($path)
        {
            if (is_dir($path)) {
                $dir =  scandir($path);
                foreach ($dir as $value) {
                    $sub_path = $path . '/' . $value;
                    if ($value == '.' || $value == '..') {
                        continue;
                    } else if (is_dir($sub_path)) {
                        $this->getDir($sub_path);
                    } else {
                        $pathinfo=pathinfo($value);
                        if(in_array($pathinfo['extension'],['jpg','png','gif','bmp','raw','jpeg'])){   //只獲取符合后綴的文件
                            $str=str_replace('../public','',$path);   //因個人需要獲取相對路徑所以去掉,不需要可以去掉
                            array_push($this->paths, $str."/{$value}");
                        }
                    }
                }
            }
        }
    php獲取指定目錄中文件路徑數組

    使用案例

    情景:替換原有小程序圖片保存圖片路徑保存了小程序緩存圖片路徑,需要修正為正確服務器相對路徑,數據量又很大,只能編寫腳本修復數據

        /**
         * @description:執行修復報名頭像路徑問題 
         * @param {type} 
         * @return: 
         */
        public function imgUpdate()
        {
            try{
                $this->paths = [];
                $this->getDir(request('path'));
                $paths=&$this->paths;
                $fileName=[];
                foreach($paths as $k =>$path){
                    $pathinfo=pathinfo($path);
                    $str=str_replace('.'.$pathinfo['extension'],'',$path);
                    $fileName=explode('_',basename($str));
                    if(count($fileName)==3){
                        $res=Registration::where(['idcard'=>$fileName[0],'name'=>$fileName[1],'subject_name'=>$fileName[2]])->update(['exam_avatar'=>$path]);
                    }
                }
                return response([
                    'code'=>200,
                    'message'=>'操作成功'
                ]);
            }catch(Exception $e){
               return response([
                   'code'=>422,
                   'message'=>$e->getMessage()
               ],422);
            }
    
           
            // dd($fileName);
            return $this->success('');
        }
        /**
         * @description: 獲取文件路徑
         * @param {type} 
         * @return: 
         */
        protected function getDir($path)
        {
            if (is_dir($path)) {
                $dir =  scandir($path);
                foreach ($dir as $value) {
                    $sub_path = $path . '/' . $value;
                    if ($value == '.' || $value == '..') {
                        continue;
                    } else if (is_dir($sub_path)) {
                        $this->getDir($sub_path);
                    } else {
                        $pathinfo=pathinfo($value);
                        if(in_array($pathinfo['extension'],['jpg','png','gif','bmp','raw','jpeg'])){   //只獲取符合后綴的文件
                            $str=str_replace('../public','',$path);   //因個人需要獲取相對路徑所以去掉
                            array_push($this->paths, $str."/{$value}");
                        }
                    }
                }
            }
        }

    版權聲明:本文內容由互聯網用戶自發貢獻,該文觀點僅代表作者本人。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如發現本站有涉嫌抄襲侵權/違法違規的內容, 請發送郵件至 舉報,一經查實,本站將立刻刪除。

    發表評論

    登錄后才能評論
    国产精品区一区二区免费