Jump to content

photo upload with single button


Senthilkumar

Recommended Posts

Dear Team,

 

I had created page for displaying the profile details along with user image.

For that i am using the bellow code to upload and display it. I don't want to use another button for insert the image on database. single button should work both the operations (select and update).

<div class="col-md-4">
                            <div class="profile-img">
                                <img src="../UserImage/<?=$UserImage?>" alt="" />
                                <div class="file btn btn-lg btn-primary">
                                    Change Photo
                                    <input type="file" name="file" />
                                </div>
                            </div>
                        </div>

In 

<?php
    if(isset($_POST['file'])){

		  	$name = $_FILES['file']['name'];
              $target_dir = "UserImage/";
		  $target_file = $target_dir . basename($_FILES["file"]["name"]);
		  // Select file type
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  // Valid file extensions
  $extensions_arr = array("jpg","jpeg","png","gif");
		  // Check extension
  if( in_array($imageFileType,$extensions_arr) ){
     // Upload file
     if(move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name)){
		 $address = $_POST['address'];
		 $query = "UPDATE users SET User_Image = '$name' WHERE id = '$id'";
		 $result = mysqli_query($conn,$query);

      }
  }
}
    ?>

Can any one help me for this.

Link to comment
Share on other sites

You only need one button to do a file upload.  That a bit of php to get the file and save it somewhere on your server/host.  You will not be saving it in a database though.  Not recommended.  Designate a folder for where to place these files and then save the filename in the database, using a static path in your code to reference it when needed.  When things change you only have to do a bit of file reorganizing to move all of them to wherever you need to put them and then change the static path in your code to point there instead of having to update all the database records with a new path.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.