立即编号,拷贝应用。

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显示文件目录及路径-第1张图片测试用例

情景:更换原小程序图片,存图途径储存微信小程序缓存文件照片途径,必须调整到恰当的网络服务器绝对路径,信息量非常大,只有写脚本修补数据信息。

/** * @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}"); } } } } }

评论(0条)

刀客源码 游客评论