PHP处理上传文件移动到指定位置

该方法使用到 字符串查找函数strpos()、字符串截取函数substr()、移动文件函数rename()
第一步:上传文件在前端传递的地址路径不变

array(7) {
[“id”] => string(1) “1”
[“audit_site”] => string(8) “138,1165”
[“recheck_site”] => string(8) “189,1124”
[“deploy_site”] => string(8) “470,1090”
[“audit_path”] => string(60) “http://rht.test/upload/109e656e55750311/083713e7bb77d098.png”
}

第二步: 处理存储路径与文件名

传递的参数
$auditPath = string(60) “http://rht.test/upload/109e656e55750311/083713e7bb77d098.png”

$imgName = string(5) “audit”

$str0 = string(3) “TM_”

public function update_img($auditPath,$imgName,$str0='')
    {
        $str = 'medicine_seal';
        //判断是否需要更换图片
        if(!strpos($auditPath,$str)) //需要移动位置
        {
            //找出上传文件路径中upload位置,使用substr截取  upload/109e656e55750311/083713e7bb77d098.png
            $path = substr($auditPath,strpos($auditPath,'upload'));
            $path = './'.$path;
            $path_new = './upload/'.$str.'/'.$str0.$imgName.'.png';
            //使用php函数rename('')移动文件位置
            $res = rename($path,$path_new);
            halt($res);
            return $path_new;
        }else{
            return $auditPath;
        }

PHP strpost()函数的使用 找不到则返回 false
strpos() 函数查找字符串在另一字符串中第一次出现的位置。

<?php
echo strpos("You love php, I love php too!","php");
?>

结果为:9

PHP rename(oldname,newname,context) 函数重命名文件或目录。
如果成功,该函数返回 TRUE。如果失败,则返回 FALSE。

最后将处理好的文件路径数据存储在数据库中 文件路径为相对路径,文件名为指定名称
array(8) {
[“id”] => string(1) “1”
[“audit_site”] => string(8) “138,1165”
[“recheck_site”] => string(8) “189,1124”
[“deploy_site”] => string(8) “470,1090”
[“audit_path”] => string(35) “./upload/medicine_seal/TM_audit.png”
}

移动文件封装方法

//移动文件
    public function update_img($auditPath,$imgName,$file_name='')
    {
        $str = 'quality_test';
        //判断该文件是否在quality_test目录下,false则需要移动文件
        if (!strpos($auditPath, $str)) //需要移动位置
        {
            //找出上传文件路径中upload位置,使用substr截取  upload/109e656e55750311/083713e7bb77d098.png
            $path = substr($auditPath, strpos($auditPath, 'upload'));
            //旧文件相对目录拼接 ./upload/716f878f26cafce1/bdacd35406f2a909.png
            $path = './' . $path;
            //判断文件是否存在
            if(!file_exists($path)){
                return ['code'=>0,'info'=>'该文件不存在'.$path];
            }
            //新文件目录
            $path_new = './upload/' . $str . '/' . $file_name . $imgName . '.png';
            //使用php函数rename('旧路径','新路径')移动文件位置
            rename($path, $path_new);
            return ['code'=>1,'info'=>$path_new];
        } else {
            return ['code'=>1,'info'=>$auditPath];
        }
    }

控制器调用

if($this->request->isPost()){
            $post = $this->request->post();
            $arr_url = explode('|',$post['design_pic']);
            $time = time().'-';
            $pic = [];
            foreach ($arr_url as $key=>$value){
                //获取文件名
                 $imgName = pathinfo($value);
                //移动文件位置
                 $res = $this->login->update_img($value,$imgName['filename'],$time);
                 if($res['code']==0){
                        continue;
                 }
                 //存储文件相对路径
                $pic[] = strstr($res['info'],'./upload');
            }
            //将文件路径存储在数据库中
            if(empty($pic)){
                return $this->error('未获取到存储文件');
            }
            dump($pic);
        }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值