如何更新个人资料图片

时间:2015-04-27 07:46:28

标签: php file-upload avatar

首次注册时,用户上传的个人资料照片会保存在www.mydomain.com/ avatar_online 文件夹中。

登录后,用户将被重定向到www.mydomain.com/ 用户文件夹。

现在进入" 用户"找到文件夹 edit_avatar.php

如何在www.mydomain.php / avatar_online 文件夹中上传新的更新个人资料图片?

在www.mydomain.com/ 用户文件夹中 edit_avatar.php 中使用代码如下:

<?
include("db.php");

$userdata=mysql_fetch_array(mysql_query("select * from user_reg where reg_id=".$_SESSION['user_id']));

//This below code is used in registration form located in www.mydomain.com root and is working.
$fileName = $_FILES["avatar"]["name"]; 
$fileNameNew = preg_replace('/\s+/', '_', $fileName);
$fileTmpLoc = $_FILES["avatar"]["tmp_name"];
// Path and file name
$pathAndName = "avatar_online/".$fileNameNew;
// Run the move_uploaded_file() function here
$moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);
// Evaluate the value returned from the function if needed




if($_POST['action']=="edit_image"){

    if(isset($_FILES['avatar'])) {
     $errors     = array();
     $maxsize    = 100000;
     $acceptable = array(
         'image/JPG',
         'image/PNG'
     );

     if(($_FILES['avatar']['size'] >= $maxsize) || ($_FILES["avatar"]["size"] == 0)) {
        $msg1 = 'Passport Image Size Is Too Large. Image Must Be Less Than 100 KB.';
     }

     if((!in_array($_FILES['avatar']['type'], $acceptable)) && (!empty($_FILES["avatar"]["type"]))) {
        $msg2= "Invalid Image Type Used For Passport Photo. Only 'JPG'  & 'PNG'  Types Are Allowed.";
     }
    }   else {


            $all_columns[]="avatar";            

            $all_values[]=addslashes($pathAndName);

            $wherefield=" reg_id";
            $wherevalue=$_SESSION['user_id'];

           $qry=$estambha->webdreamupdate('user_reg',$all_columns,$all_values,$wherefield,$wherevalue,'');
        echo mysql_error();

        header("location:edit_avatar_success.php"); 


   }
  }

 ?>

和Html表格如下:

 <form name="edit_image_form" id="edit_image_form" action="" method="post"  enctype="multipart/form-data" onSubmit="return validate_editmember()">

    <input type="hidden" name="action" value="edit_image" />


<span>New Profile Photo </span>
      <input type="file" size="40" data-max-size='100kb' class='demo' data-type='image' name="avatar"  id="avatar" required/>


     <input name="submit" type="submit" value="Edit & Save Profile Photo" />

 </form>

0 个答案:

没有答案
相关问题