kasthuri S kasthuri S - 6 months ago 9
PHP Question

Not getting bootstrap file value using PHP

I am using bootstrap file upload. I have one form field called photo upload. I already inserted the file value in the database and moved it into a folder. Now I want to edit this photo. user edit the lastname and change photo means that time it will work fine,but user only change the last name that time it not working properly,because file name value is getting null, I am trying this method,How can I do this?

<?php
$sql = mysql_query("SELECT * FROM task_employee WHERE emp_email='".$_SESSION['email']."'");
while($edit = mysql_fetch_assoc($sql))
{
?>
<form class="form-horizontal" novalidate="novalidate" method="POST" id="newUserForm">
<div class="tab-content" style="margin:15px">
<div id="w2-account" class="tab-pane active">


<div class="form-group">
<label class="col-md-3 control-label">Last Name</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="lname" name="lname" value="<?php echo $edit['emp_lastname'];?>" placeholder="Enter your Lastname">

</div>
</div>



<div class="form-group">
<label class="col-md-3 control-label">Photo Upload</label>
<div class="col-md-6">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="input-append">
<div class="uneditable-input">
<span class="fileupload-preview"><?php echo $edit['emp_main_photo'];?></span>
</div>
<span class="btn btn-default btn-file">
<span class="fileupload-exists">Change</span>
<span class="fileupload-new">Select file</span>
<input type="file" id="file" name="file" value="<?php echo $edit['emp_main_photo'];?>">
</span><!--d42c4f0d9fcc1b1bff87fe8ba80b1605.jpg-->
</div>
</div>
</div>
</div>

<div class="form-group">
<input type="submit" name="user-submit" id="user-submit">
</div>

</form>
<?php } ?>


<script type="text/javascript">
$(document).ready(function(){
$('#user-submit').click(function(event){
event.preventDefault();
if($('#newUserForm').valid()){
var formData = new FormData();
var formData = new FormData($('#newUserForm')[0]);
formData.append('file', $('input[type=file]')[0].files[0]);
$.ajax({
url: 'php/profile_update.php',
type: 'POST',
data: formData,
dataType: 'json',
async: false,
cache: false,
contentType: false,
processData: false,
success: function (data) {
console.log(data);// here i am getting null value for $filename

},
});
}
});
});
</script>


profile_update.php

<?php
$lstname=$_POST['lname'];//i am getting value here
$filename = basename($_FILES['file']['name']);// i am not getting value here
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$new_name= md5($filename.time()).'.'.$extension;
$update = mysql_query("UPDATE task_employee SET emp_lastname='$lstname',emp_main_photo = '$new_name' WHERE emp_id='".$_SESSION['emp_id']."'");
?>

Answer

Why don't you have write a condition over it like if user changed photo then you will get its name but if the user doesn't you will get that field empty.

    if(isset($_FILES['file']) && !empty($_FILES['file']['name'])){
     $filename  = basename($_FILES['file']['name']);// i am not getting value here
     $extension = pathinfo($filename, PATHINFO_EXTENSION);
     $new_name= md5($filename.time()).'.'.$extension;
     $update = mysql_query("UPDATE task_employee SET emp_lastname='$lstname',emp_main_photo = '$new_name' WHERE emp_id='".$_SESSION['emp_id']."'");
   }else{
     $update = mysql_query("UPDATE task_employee SET emp_lastname='$lstname' WHERE emp_id='".$_SESSION['emp_id']."'");
   }
Comments