将图像存储到php中的文件夹中,无需单独的上传按钮


storing images to folder in php without seperate upload button

我是php新手,如果我傻了,请原谅我。

我有一个表格,在提交时会更新表格中的更改。

现在我添加了一个图像上传字段,字段类型为文件。我的问题是,我不想通过单独点击上传按钮来上传,相反,我想使用我用于表单更新的旧提交按钮。我试了好几种方法,但都失败了。此外,我只想以名称admin保存,并且它应该只允许.jpg扩展

我的表单页面代码如下

 <?php
         if(isset($_POST['update'])) {
            $info = pathinfo($_FILES['imagefile']['name']);
            $ext = $info['extension']; // get the extension of the file
            $newname = "admin.".$ext; 
            $target = WEB_URL .'img/'.$newname;
            move_uploaded_file( $_FILES['imagefile']['tmp_name'], $target);
            $name_a = $_POST['name'];
            $email_a = $_POST['email'];
            $pass_a = $_POST['password'];
$sql = "UPDATE admin SET a_name = '$name_a', a_email = '$email_a', password = '$pass_a' where aid='$update_id' ";
            $retval = mysql_query($sql,$link);
            if(! $retval ) {
               die('Could not update data: ' . mysql_error());
            }
           // echo "Updated data successfully'n";
         } 
$result = mysql_query("SELECT * FROM admin where aid='$update_id' ",$link);
    while($row = mysql_fetch_array($result)){
        $name = $row['a_name'];
        $email = $row['a_email'];
        $password = $row['password'];
    }
     mysql_close($link);
            ?>
      <div class="box box-widget widget-user-2">
       <div class="widget-user-header bg-yellow">
              <div class="widget-user-image">
                 <?php echo '<img src="' . $img . '" class="img-circle" alt="User Image">'; ?>
              </div>
              <!-- /.widget-user-image -->
              <h3 class="widget-user-username"><?php echo "$name"; ?></h3>
              <h5 class="widget-user-desc"><?php echo "$role"; ?></h5>
            </div>
            <div class="box-footer no-padding">
              <form role="form" method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
              <div class="box-body">
              <div class="form-group">
                  <label for="exampleInputName1">Name</label>
                  <input type="text" class="form-control" id="exampleInputName1" name="name" value="<?php echo $name; ?>">
                </div>
                <div class="form-group">
                  <label for="exampleInputEmail1">Email address</label>
                  <input type="email" class="form-control" id="exampleInputEmail1" name="email" value="<?php echo $email; ?>">
                </div>
                <div class="form-group">
                  <label for="exampleInputPassword1">Password</label>
      <input type="password" class="form-control" id="exampleInputPassword1" name="password" value="<?php echo $password; ?>">
                </div>
                <div class="form-group">
                  <label for="exampleInputFile">Profile picture</label><br/>
                  <img id="preview" src="<?php echo $img; ?>" /><br/><br/>
                  <input type="file" onchange="readURL(this);" id="exampleInputFile" name="imagefile">
                </div>
              </div>
              <!-- /.box-body -->
              <div class="box-footer">
                <button type="submit" name="update" id="update"  class="btn btn-primary">Submit</button>
              </div>
            </form>
            </div>
          </div>
          <!-- /.widget-user -->

我尝试过但没有达到我的要求的线路如下。这也在上面的代码中。

$info = pathinfo($_FILES['imagefile']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = "admin.".$ext; 
$target = WEB_URL .'img/'.$newname;
move_uploaded_file( $_FILES['imagefile']['tmp_name'], $target);

您忘记将enctype="multipart/form-data"写入到<form>中,如果没有此属性,则无法上载任何图像。

您应该更改:

<form role="form" method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">