Senthilkumar Posted December 17, 2022 Share Posted December 17, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315657-photo-upload-with-single-button/ Share on other sites More sharing options...
ginerjm Posted December 17, 2022 Share Posted December 17, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315657-photo-upload-with-single-button/#findComment-1603675 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.