php 上传图片后端代码

 //上传图片
    public function uploadp()
    {
    	// dump($_FILES);die;
         extract($_POST);
    	 $file_name = $_FILES['file']['name'];
    	
    	 $type = array("jpg", "gif", 'png', 'bmp');
    	  $ext = explode(".", $file_name);
          $ext = $ext[count($ext) - 1];//取图片的后缀名
    	   if (in_array($ext,$type)){
    	   	 do{
		        $new_name = $this->get_file_name(6).'.'.$ext;//新的图片名称
		        
		        $path='uploads/imgs/'.$new_name;//uploads为目标文件夹
		       
		    }while (file_exists("../" . $path));//检查图片是否存在文件夹,存在返回ture,否则false
	
			    $temp_file=$_FILES['file']['tmp_name']; 
			     //执行数据库存储
			     $result = [
			     		'update_time'=>date('Y-m-d H:i:s'),
			     		'picture_urls'=>$path,
			     	];
			 
    	      $data = M('imgs')->add($result);
			  if($data > 0 ){
	        		 move_uploaded_file($temp_file,$path);//移动临时文件到目标路径
	        		 $arr = [
	        		 	'pid'=>$data
	        		 	];
	        		  $this->ajaxReturn($arr,'JSON');
		        }else{
		            $this->error('上传失败,请重试!');
		        }
    	   }else{
    	   		$this->error('上传失败!格式不正确!');
    	   }
    }

猜你喜欢

转载自blog.csdn.net/weixin_45609681/article/details/104988530