I am trying to upload an image using php and ajax. I have tried coding but i am not sure that how can i pass ectype data of the file which i am going to upload to AJAX and then pass it in PHP where i am uploading the file and saving the path of the file into the database. Here is my html and AJAX code.
<form class="form-horizontal" role="form" onsubmit="uploadimage(this.value)" method="post" id= "sample" enctype="multipart/form-data">
<input type="hidden" name="sid" value="<?php echo $sid; ?>">
<input type="hidden" name="site" value="<?php echo $site; ?>">
<input type="hidden" name="img_pos" value="img_gal1">
<div class="form-group">
<!--<label class="control-label col-lg-2">Upload Gallery</label><br/>-->
<div class="col-lg-10">
<input type="file" name="fileToUpload" placeholder="Upload Gallery">
<!--<input type="Submit" value="Upload Image" name="submit">-->
</div>
</div>
<div class="form-group">
<div class="col-sm-5">
<input type="submit" class="btn btn-primary form-control">
</div>
</div>
</form>
and ajax code is:
<script>
function uploadimage(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gallery_upload.php?q="+str,true);
xmlhttp.send();
}
I just copied the code from w3schools. I am not sure what the ajax code should be regarding the passing of a 2 dimensional file array so I didn't edited it. The php code looks like this for reference.
<?php
$img_pos= $_REQUEST["img_pos"];
$folder= "img/$sid/";
if (!file_exists($folder))
{
mkdir($folder, 0777, true);
$target_dir = $folder;
}
else
{
$target_dir = $folder;
}
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$n_filename= $target_dir.$img_pos.".".$imageFileType;
rename($target_file, $n_filename);
$uploadOk = 1;
//$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
//echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
//echo "File is not an image.";
$uploadOk = 0;
}
}
/*Check if file already exists
if (file_exists($target_file)) {
header("Location: admin/pages/profile.php?response= Sorry, file already exists. PLease change the name and try again!&sid=".$sid);
//echo "Sorry, file already exists.";
$uploadOk = 0;
}*/
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
header("Location: admin/pages/profile.php?response= Sorry, your file is too large.&sid=".$sid);
//echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
header("Location: admin/pages/profile.php?response= Sorry, only JPG, JPEG, PNG & GIF files are allowed.&sid=".$sid);
//echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
header("Location: admin/pages/profile.php?response= Sorry, your file was not uploaded.&sid=".$sid);
//echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
$query="Update content SET ".$img_pos."='".$_FILES["fileToUpload"]["name"]."' where Sid='".$sid."' and site='".$site."'";
$result= mysqli_query($con,$query) or die(mysqli_error($con));
if($result)
{
header("Location: admin/pages/profile.php?response= Great! Your Profile has been updated&sid=".$sid);
//echo "done";
//echo $query;
}
} else {
header("Location: admin/pages/profile.php?response= Sorry, there was an error uploading your file.&sid=".$sid);
//echo "Sorry, there was an error uploading your file.";
}
}
?>
I want to upload 5 images from page. So I have 5 different forms. and I was thinking to make only one php code for uploading the images by using AJAX. so basically what I want is that I transfer the file type data to AJAX and then AJAX works with php and uploads the image to the folder and updates the path in database. So I basically want to know that what would be the AJAX code to send the data in php.