storing images to folder in php without seperate u

2019-07-25 09:28发布

i am newbie in php kindly pardon me if i am silly .

I have a form which on submit updates the changes in the form .

Now i added a image upload field with field type as file . my question is i don't want it to be uploaded by clicking upload button separately, instead i want to use the same old submit button which i use for form update . i tried several ways but failed . also i want to save only in the name admin and it should allow only .jpg extension

My form page code is as follows

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

The lines which i tried and failed to achieve my requirement is as follows . which is also there in the above code .

$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);

标签: php image forms
1条回答
Lonely孤独者°
2楼-- · 2019-07-25 10:16

You forgot to write enctype="multipart/form-data" into your <form> without this attribute you can not upload any image.

You should change :

<form role="form" method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
查看更多
登录 后发表回答