如何在php5中上传文件并在数据库中保存文件名(mysql)

xzabzqsa  于 2021-06-19  发布在  Mysql
关注(0)|答案(0)|浏览(213)

新手在这里与php和尝试学习。我有一个php代码,似乎不工作。。。至少有一部分。
我想将文件名存储在数据库中,并将文件存储在文件夹中。这些是我一直在研究的代码的一部分。。。
html格式:

<div class="form-group">
    <img src="images/<?php if(file_exists('images/avatar/'.$session->userinfo['avatar'])){ echo 'avatar/'.$session->userinfo['avatar']; }else{ echo 'avatar1.png';}?>" class="img-circle" width="150">
    <input type="file" name="avatar" class="form-control"/>

    <label class="sr-only">Email</label>
    <input type="text" name="email" maxlength="50" value="<?php if($form->value("email") == ""){ echo $session->userinfo['email']; }else{ echo $form->value("email"); }?>" class="form-control"  placeholder="Email Address">

    <label class="sr-only">Current Password</label>
    <input type="password" name="curpass" maxlength="30" value="<?php echo $form->value("curpass"); ?>"  class="form-control custom-middle"  placeholder="Current Password">

    <label class="sr-only">New Password</label>
    <input type="password" name="newpass" maxlength="30" value="<?php echo $form->value("newpass"); ?>" class="form-control"  placeholder="New Password">
  </div>

  <div class="form-group">
    <textarea class="form-control" name="bio" rows="5" maxlength="250" placeholder="Bio" style="resize: none;"><?php if($form->value("bio") == ""){ echo $session->userinfo['bio']; }else{ echo $form->value("bio"); }?></textarea>
  </div>

  <input type="hidden" name="subedit" value="1">
  <input class="btn btn-lg btn-primary btn-block" type="submit" value="Save Changes">

</form>

php(updateuserfield()函数的来源)

function updateUserField($username, $field, $value){
  $q = "UPDATE ".TBL_USERS." SET ".$field." = '$value' WHERE username = '$username'";
  return mysql_query($q, $this->connection);
}

进程.php

function procEditAccount(){
  global $session, $form;
  /* Account edit attempt */
  $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email'], $_POST['bio'], $_POST['avatar']);

  /* Account edit successful */
  if($retval){
     $_SESSION['useredit'] = true;
     header("Location: ".$session->referrer);
  }
  /* Error found with form */
  else{
     $_SESSION['value_array'] = $_POST;
     $_SESSION['error_array'] = $form->getErrorArray();
     header("Location: ".$session->referrer);
  }

}
session.php*(editaccount()函数的来源)*

function editAccount($subcurpass, $subnewpass, $subemail, $subbio, $subavatar){
  global $database, $form;  //The database and form object

//working codes.....
//then below is the problem....

/* Change Avatar */
  if($subavatar){
    $subavatar = $_POST['avatar'];
    $folder = "../images/avatar/";

    move_uploaded_file($_FILES["avatar"]["tmp_name"], $folder . $_FILES["avatar"]["name"]);
    $database->updateUserField($this->username,"avatar",$subavatar);
  }
}

你可能想知道我为什么用 $subavatar = $_POST['avatar']; 而不是 $_FILES["avatar"]["name"]; 这是因为,它不会保存在数据库中,我需要的文件名。但是,这只会将名称存储在数据库中,而不会将文件保存在文件夹中。
有没有人能帮我弄清楚,告诉mne我做错了什么,怎么解决。请,谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题