Home  >  Article  >  Backend Development  >  Introduction to PHP background image upload works

Introduction to PHP background image upload works

不言
不言Original
2018-07-05 13:43:181941browse

This article mainly introduces the PHP background image uploading works, which has certain reference value. Now I share it with everyone. Friends in need can refer to it.

//把新图片添加到文件夹里
   public function info($file=''){
        $info = $file->validate(['ext'=>'jpg'])->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'uploads'); // 移动到框架应用根目录/public/uploads/ 目录下
        if($info){             
           $info->getExtension();// 输出 jpg 
           $pic=$info->getFilename();// 输出 42a79759f284b767dfcb2a0197904287.jpg11.  
           return $pic;    
        }else{      
           echo $file->getError();// 上传失败获取错误信息   
        }
   }
/**
    * 作品添加
    * name  谁的作品
    * tel  电话
    * img1 img2 img3 img4 作品图片
    * address  拍摄地点
    * addtime  拍摄时间
    * picname 作品名称
    * vote 投票情况
    */
   public function zadd(){
       $param=input('param.');
       if(empty($param['tel'])){
           return json(['code'=>2,'message'=>'tel不能为空']);
       }
$files = request()->file('img1');
       $files = [];
       $param = [];
       foreach ($files as $key => $file) {
           $num = $key+1;  //$key 下标
           $img = 'img'.$num; //例如:img1
           $param[$img] = $this->info($file);
       }
       $a=db('user')->where('tel',$param['tel'])->find();
       if(empty($a)){
           $data=db('user')->insertGetId(['name'=>$param['name'],'tel'=>$param['tel'],'img1'=>$param['img1'],'img2'=>$param['img2'],'img3'=>$param['img3'],'img4'=>$param['img4'],'address'=>$param['address'],'addtime'=>$param['addtime'],'picname'=>$param['picname'],'vote'=>$param['vote']]);
           if($data){
               return json(['code'=>1,'message'=>'作品添加成功']);
           }else{
               return json(['code'=>2,'message'=>'作品添加失败']);
           }
       }else{
           return json(['code'=>3,'message'=>'该手机号已存在']);
       }
   }
/**
    * 作品列表
    * page 页数  
    * num  条数
    */
   public function list(){
      $param=input('param.');
      if(empty($param['page'])){
           $param['page']=1;
      }
if(empty($param['num'])){
           $param['num']=5;
      }
$data=db('user')->page($param['page'],$param['num'])->select();
      return json(['code'=>1,'data'=>$data]);
   }

The above is the entire content of this article. I hope it will be helpful to you. Everyone’s learning is helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

PHP uses the mysql_insert_id() function to obtain the ID of the data just inserted or the currently published article

Common in PHP Magic method functions and usage

The above is the detailed content of Introduction to PHP background image upload works. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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