User avatar upload

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

a. It must be submitted in post mode, because the browser will The file is binary encoded, and binary encoding cannot be transmitted in the URL address bar, so you cannot use the get method to submit

b. You must add enctype="multipart/form-data" to tell the browser to upload the data It is the file data

portrait.php code is as follows:

1,'name'=>'张三'); ?> 

上传用户头像

用户姓名:

现有头像:


上传头像:

The above code is to rename the uploaded image based on the user id. For example, the user id is 1 and the name of the saved image is for 1.jpg;? "?rand=rand()" is an operation to prevent caching. The value in onerror is "this.src='./default.jpg'", which means the picture is displayed by default when there is no picture

微信图片_20180228143443.png

2, display basic user information

##1,'name'=>'Zhang San'); ?>


3, view the received uploaded file data

';

print_r($_FILES);
echo '';?>

4, receive and process uploaded images

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:

portrait.php :

1,'name'=>'张三'); echo '
'; 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; } } ?>

编辑用户头像

用户姓名:

现有头像:


上传头像:

View the running results:

微信图片_20180228151241.png

Continuing Learning
||
1,'name'=>'张三'); echo '
'; 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; } } ?>

编辑用户头像

用户姓名:

现有头像:


上传头像:
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!