今天做项目中,用户的头像需要可以自定义,所以就写好代码了

HTML表单

<form action='/upload.php' method='post' enctype="multipart/form-data">
    <input type='file' name='file' accept="image/jpeg" >
    <div style="display:none">
        <input type="text" name='username' value='username'>
    </div>
     <button type="submit">更换头像</button>
    </form>

PHP upload

<?php
if (isset($_FILES["file"]))
{       
  $file = $_FILES['file']; 

 $uploaddir="/upload/image/";               

     $UploadSuccess =move_uploaded_file($file['tmp_name'],".".$uploaddir.time());

     if ($UploadSuccess)

     {
         $imgurl=$uploaddir.time();
         $username = $_POST['username'];
     //    echo $username;
  $conn = mysqli_connect("localhost","open","open","open");
  mysqli_query($conn,"update `think_user` set `headimgurl`='$imgurl' where `username`='$username'");
       //链接数据库修改用户图片地址

       }
  }
  header("refresh:0;url='../'");
Last modification:November 25th, 2020 at 09:18 pm
你的支持是我最大的动力,谢谢