Here is my problem. I tried to upload image and I store images in corresponding folder and image path in database with the help of this URL.
http://www.lionblogger.com/how-to-upload-file-to-server-using-php-save-the-path-in-mysql/.
When i tried that way i will get correct answer. After i tried same code with AJAX is not working properly. I dont know what mistake I did. Below is my code.
HTML code
<div class="input-group form-group">
<label> Upload Your Photo </label>
<input type="file" name="upload_photo" id="upload_photo">
</div>
<div class="">
<input type="submit" class="btn btn-success btn-lg " name="upload_files" id="upload_files" value="UPLOAD" >
</div>
AJAX code
$("#upload_files").click(function(event){
event.preventDefault();
var upload_photo1 = $('#upload_photo').val();
var photo= upload_photo1.split('\\').pop().split('/').pop();
var datas="photo="+photo;
alert(datas);
if(photo==''){
sweetAlert({
title: "WARNING!!!",
text: "Please Upload All Corresponding Documents And Try Again !!!!",
type: "warning"
});
} else {
$.ajax({
type: "POST",
url: 'php/upload_files.php',
data:datas
}).done(function( data ) {
alert(data);
});
}
});
And my PHP file Upload_files.php
<?php
$fileExistsFlag = 0;
$fileName = $_POST['photo'];
var_dump($fileName);
$link = mysqli_connect("localhost","root","","spark") or die("Error ".mysqli_error($link));
$query = "SELECT filename FROM filedetails WHERE filename='$fileName'";
$result = $link->query($query) or die("Error : ".mysqli_error($link));
while($row = mysqli_fetch_array($result)) {
if($row['filename'] == $fileName) {
$fileExistsFlag = 1;
}
}
if($fileExistsFlag == 0)
{
$target = "files/";
$fileTarget = $target.$fileName;
$tempFileName = $_FILES["fileName"]["tmp_name"];
$fileDescription = $_POST['Description'];
$result = move_uploaded_file($tempFileName,$fileTarget);
$ext = end(explode('.', $fileName));
if ($_FILES["fileName"]["size"] > 2097152)
{
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
else if($ext != "jpg" && $ext != "png" && $ext != "jpeg"&& $ext != "gif" )
{
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
else
{
if($result) {
echo "Your file <html><b><i>".$fileName."</i></b></html> has been successfully uploaded";
$query = "INSERT INTO filedetails(filepath,filename,description) VALUES ('$fileTarget','$fileName','$fileDescription')";
$link->query($query) or die("Error : ".mysqli_error($link));
}
else {
echo "Sorry !!! There was an error in uploading your file";
}
}
mysqli_close($link);
}
else {
echo "File <html><b><i>".$fileName."</i></b></html> already exists in your folder. Please rename the file and try again.";
mysqli_close($link);
}
?>
Please Bear my doubts.. Beginner of PHP.Kindly Help me out of this problem.