User avatar upl...LOGIN

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:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/28 0028
 * Time: 下午 1:18
 */
header('Content-type:text/html;charset=utf-8');
$info=array('id'=>1,'name'=>'张三');
?>
<form action="" method="post" enctype="multipart/form-data">
    <h2>上传用户头像</h2>
    <p>用户姓名:<?php echo $info['name'];?></p>
    <p>现有头像:</p><img src="<?php echo './'.$info['id'].'.jpg?rand='.rand() ;?>"/><br>
    上传头像:<input name="pic" type="file"><br>
<!--文件上传表单-->
    <input type="submit" value="保存头像">
</form>

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

##<?php $info=array('id' =>1,'name'=>'Zhang San'); ?>


3, view the received uploaded file data

<?php echo '<pre>';

print_r($_FILES);
echo '</pre>';?>

4, receive and process uploaded images

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

portrait.php :

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/28 0028
 * Time: 下午 1:18
 */
header('Content-type:text/html;charset=utf-8');
$info=array('id'=>1,'name'=>'张三');
echo '<pre>';
print_r($_FILES);
echo '</pre>';
//接收并处理上传图像
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;
    }
}
?>
<form action="" method="post" enctype="multipart/form-data">
    <h2>编辑用户头像</h2>
    <p>用户姓名:<?php echo $info['name'];?></p>
    <p>现有头像:</p><img src="<?php echo './'.$info['id'].'.jpg?rand='.rand() ;?>"/><br>
    上传头像:<input name="pic" type="file"><br>
<!--文件上传表单-->
    <input type="submit" value="保存头像">
</form>

View the running results:

微信图片_20180228151241.png
Next Section

<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/2/28 0028 * Time: 下午 1:18 */ header('Content-type:text/html;charset=utf-8'); $info=array('id'=>1,'name'=>'张三'); echo '<pre>'; print_r($_FILES); echo '</pre>'; //接收并处理上传图像 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; } } ?> <form action="" method="post" enctype="multipart/form-data"> <h2>编辑用户头像</h2> <p>用户姓名:<?php echo $info['name'];?></p> <p>现有头像:</p><img src="<?php echo './'.$info['id'].'.jpg?rand='.rand() ;?>"/><br> 上传头像:<input name="pic" type="file"><br> <!--文件上传表单--> <input type="submit" value="保存头像"> </form>
submitReset Code
ChapterCourseware