I am using php mysql and uploading image using ajax( jquery). The image is being uploaded successfully but is not being dispalyed immediately, on reloding the page it displaying correctly.
The upload and display are working when I am using this code..
<?php
// Set the upload folder path
$target_path = "uploads/";
// Set the new path with the file name
$target_path = $target_path . basename( $_FILES['myfile']['name']);
// Move the file to the upload folder
if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
// print the new image path in the page, and this will recevie the javascripts 'response' variable
echo $target_path;
} else{
// Set default the image path if any error in upload.
echo "default.jpg";
}
?>
But instead of uploading the image to upload directory with the original name of the image which is being uploaded I want to change the name on upload to image01.jpg and if the image with this name is present then I want it to get replaced with the new one. For this I use this code...
<?php
// Set the upload folder path
$place_file="";
$target_path = "1/";
$target_path1 = "1/image01.jpg";
// Set the new path with the file name
$target_path = $target_path ."image01.jpg";
// Move the file to the upload folder
$place_file=move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path1);
echo $target_path;
?>
The image is uploding fine but it is not being displayed immediately but gets displayed on page refresh. It displays the previous image only and I think the replacing of image is causing a problem.
I am new in file uploading coding thats why having problem.
This js code uploads the image
new AjaxUpload(button,{
action: 'upload.php',
name: 'myfile',
onSubmit : function(file, ext){
spinner.css('display', 'block');
// you can disable upload button
this.disable();
},
onComplete: function(file, response){
button.stop(false,true).fadeOut(200);
spinner.css('display', 'none');
$('#profile_img').attr('src', response);
// enable upload button
this.enable();
}
});
The above php code was upload.php