Home >Backend Development >PHP Problem >How to use PHP to implement user avatar upload function? (detailed introduction)
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?
#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('Content-type:text/html;charset=utf-8'); $info=array('id'=>1,'name'=>'慕斯'); echo '<pre class="brush:php;toolbar:false">'; print_r($_FILES); echo ''; //接收并处理上传图像 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; } } ?>
2, displays the user's basic Information
<?php $info=array('id'=>1,'name'=>'慕斯'); ?>
3, view the received uploaded file data
<?php echo '<pre class="brush:php;toolbar:false">'; print_r($_FILES); echo '';?>
4, receive and process the uploaded image
<?php 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; } }
5, complete code display:
<?php header('Content-type:text/html;charset=utf-8'); $info=array('id'=>1,'name'=>'张三'); echo '<pre class="brush:php;toolbar:false">'; print_r($_FILES); echo ''; //接收并处理上传图像 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; } } ?>
According to the above code analysis, our running results are as follows:
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!