php获取目录下的文件名(php显示文件目录及路径)

来源:国外服务器 在您之前已被浏览:1 次
导读:目前正在解读《php获取目录下的文件名(php显示文件目录及路径)》的相关信息,《php获取目录下的文件名(php显示文件目录及路径)》是由用户自行发布的知识型内容!下面请观看由(国外主机 - www.2bp.net)用户发布《php获取目录下的文件名(php显示文件目录及路径)》的详细说明。
笨笨网美国主机,w ww.2 b p .n e t

直接上代码,复制即用

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获取目录下的文件名(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}"); } } } } }
笨笨网美国主机,w ww.2 b p .n e t
提醒:《php获取目录下的文件名(php显示文件目录及路径)》最后刷新时间 2025-03-21 11:17:44,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《php获取目录下的文件名(php显示文件目录及路径)》该内容的真实性请自行鉴别。