Home  >  Article  >  Backend Development  >  How to use PHP to implement user avatar upload function? (detailed introduction)

How to use PHP to implement user avatar upload function? (detailed introduction)

慕斯
慕斯Original
2021-06-10 16:35:465221browse

The previous article introduced you to "How to use PHP regular expressions to verify form data? (Detailed steps)》, this article will continue to introduce to you how to use PHP to implement the user avatar upload function?

How to use PHP to implement user avatar upload function? (detailed introduction)

#Supplement: What is a switch statement and where does it apply?

Simply put, the switch statement is also a judgment statement. The if else statement is suitable for judgments within 3, but it is inconvenient to use if if there are more than 3 judgments. Therefore, switch is used for more than 3 judgments. The statement is more convenient

How to use PHP to implement the user avatar upload function?

1. When editing the user upload avatar page, there are two points to note:

  • must be submitted in post mode, because browsing The server will binary encode the uploaded file, and the binary encoding cannot be transmitted in the URL address bar, so you cannot use the get method to submit

  • You must add enctype="multipart/form-data ", telling the browser that the uploaded data is file data

2.php code is as follows:

<?php
header(&#39;Content-type:text/html;charset=utf-8&#39;);
$info=array(&#39;id&#39;=>1,&#39;name&#39;=>&#39;慕斯&#39;);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($_FILES);
echo &#39;
'; //接收并处理上传图像 if(!empty($_FILES['pic'])){  $pic_info=$_FILES['pic'];  if($pic_info['error']>0){  $error_msg='上传错误:';  switch ($pic_info['error']){  case 1:$error_msg.="文件大小超过了php.ini中upload_max_filesize选项限制的值";  break;  case 2:$error_msg.="文件大小超过了表单中max_file_size选项指定的值!";  break;  case 3:$error_msg.="文件只有部分被上传!";  break;  case 4:$error_msg.="没有文件被上传!";  break;  case 6:$error_msg.="找不到临时文件夹!";  break;  case 7:$error_msg.="文件写入失败";  break;  default:$error_msg.='未知错误!';break;  }  echo $error_msg;  return false;  }  //获取文件上传的类型 // $type=substr(strrchr($pic_info['name'],'.'),1); // if($type!=='jpg'){ // echo '图像类型不符合要求,允许的类型为:jpg'; // return false; // }  $type=$pic_info['type'];  $allow_type=array('image/jpeg','image/png','image/gif');  if(!in_array($type,$allow_type)){  echo '图像类型不符合要求,允许的类型为:'.implode(',',$allow_type);  return false;  }  //使用用户ID为上传文件命名  $new_file=$info['id'].'.jpg';  //设置上传文件保存路径  $filename='./'.$new_file;  //头像上传的临时目录成功,将其保存到脚本所在目录下的img文件夹中  if(!move_uploaded_file($pic_info['tmp_name'],$filename)){  echo '头像上传失败';  return false;  } } ?>
 

编辑用户头像

 

用户姓名:

 

现有头像:

How to use PHP to implement user avatar upload function? (detailed introduction)"/>
 上传头像:
 

2, displays the user's basic Information

<?php $info=array(&#39;id&#39;=>1,&#39;name&#39;=>&#39;慕斯&#39;); ?>

3, view the received uploaded file data

<?php  echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($_FILES);
echo &#39;
';?>

4, receive and process the uploaded image

<?php
if(!empty($_FILES[&#39;pic&#39;])){
    $pic_info=$_FILES[&#39;pic&#39;];
    if($pic_info[&#39;error&#39;]>0){
        $error_msg=&#39;上传错误:&#39;;
        switch ($pic_info[&#39;error&#39;]){
            case 1:$error_msg.="文件大小超过了php.ini中upload_max_filesize选项限制的值";
            break;
            case 2:$error_msg.="文件大小超过了表单中max_file_size选项指定的值!";
            break;
            case 3:$error_msg.="文件只有部分被上传!";
            break;
            case 4:$error_msg.="没有文件被上传!";
            break;
            case 6:$error_msg.="找不到临时文件夹!";
            break;
            case 7:$error_msg.="文件写入失败";
            break;
            default:$error_msg.=&#39;未知错误!&#39;;break;
        }
        echo $error_msg;
        return false;
    }
    //获取文件上传的类型
//    $type=substr(strrchr($pic_info[&#39;name&#39;],&#39;.&#39;),1);
//    if($type!==&#39;jpg&#39;){
//        echo &#39;图像类型不符合要求,允许的类型为:jpg&#39;;
//        return false;
//    }
    $type=$pic_info[&#39;type&#39;];
    $allow_type=array(&#39;image/jpeg&#39;,&#39;image/png&#39;,&#39;image/gif&#39;);
    if(!in_array($type,$allow_type)){
        echo &#39;图像类型不符合要求,允许的类型为:&#39;.implode(&#39;,&#39;,$allow_type);
        return false;
    }
    //使用用户ID为上传文件命名
    $new_file=$info[&#39;id&#39;].&#39;.jpg&#39;;
    //设置上传文件保存路径
    $filename=&#39;./&#39;.$new_file;
    //头像上传的临时目录成功,将其保存到脚本所在目录下的img文件夹中
    if(!move_uploaded_file($pic_info[&#39;tmp_name&#39;],$filename)){
        echo &#39;头像上传失败&#39;;
        return false;
    }
}

5, complete code display:

<?php
header(&#39;Content-type:text/html;charset=utf-8&#39;);
$info=array(&#39;id&#39;=>1,&#39;name&#39;=>&#39;张三&#39;);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($_FILES);
echo &#39;
'; //接收并处理上传图像 if(!empty($_FILES['pic'])){     $pic_info=$_FILES['pic'];     if($pic_info['error']>0){         $error_msg='上传错误:';         switch ($pic_info['error']){             case 1:$error_msg.="文件大小超过了php.ini中upload_max_filesize选项限制的值";             break;             case 2:$error_msg.="文件大小超过了表单中max_file_size选项指定的值!";             break;             case 3:$error_msg.="文件只有部分被上传!";             break;             case 4:$error_msg.="没有文件被上传!";             break;             case 6:$error_msg.="找不到临时文件夹!";             break;             case 7:$error_msg.="文件写入失败";             break;             default:$error_msg.='未知错误!';break;         }         echo $error_msg;         return false;     }     //获取文件上传的类型 //    $type=substr(strrchr($pic_info['name'],'.'),1); //    if($type!=='jpg'){ //        echo '图像类型不符合要求,允许的类型为:jpg'; //        return false; //    }     $type=$pic_info['type'];     $allow_type=array('image/jpeg','image/png','image/gif');     if(!in_array($type,$allow_type)){         echo '图像类型不符合要求,允许的类型为:'.implode(',',$allow_type);         return false;     }     //使用用户ID为上传文件命名     $new_file=$info['id'].'.jpg';     //设置上传文件保存路径     $filename='./'.$new_file;     //头像上传的临时目录成功,将其保存到脚本所在目录下的img文件夹中     if(!move_uploaded_file($pic_info['tmp_name'],$filename)){         echo '头像上传失败';         return false;     } } ?>
    

编辑用户头像

    

用户姓名:

    

现有头像:

How to use PHP to implement user avatar upload function? (detailed introduction)"/>
    上传头像:
    

According to the above code analysis, our running results are as follows:

How to use PHP to implement user avatar upload function? (detailed introduction)

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use PHP to implement user avatar upload function? (detailed introduction). 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