Why do I get this error? Am I missing code fragments? If so please give a simple explanation. I am very beginner to ajax and jQuery. I already searched Google. But many of them are technical about image uploading that I don't understand. Help me. thanks.
$(document).ready( function(e) {
$(".frmUpload").on('submit',(function(e) {
e.preventDefault();
$(".upload-msg").text('Loading...');
$.ajax({
url: "processor2.php",
type: "POST",
success: function(data){ //function to be called if request succeeds
$(".upload-msg").html(data);
}
});
}));
});
<h1>Ajax Image Upload</h1>
<form action="" method="post" class="frmUpload">
<input type="file" name="photoUpload" id="ajaxUpload">
<input type="submit" value="UPLOAD">
</form>
<div class="img-preview"></div>
<div class="upload-msg"></div>
PHP script is shown below;
<?php
// photoUpload was given to name attribute of the upload field in the form
if( is_uploaded_file($_FILES['photoUpload']['tmp_name']) && $_FILES['photoUpload']['error']==0 ) {
echo "The file was uploaded successfully to the temporary location, but has not been saved<br>";
echo "<br>The file is temporary stored: ". $_FILES['photoUpload']['tmp_name'];
echo "<br>The file name was: ". $_FILES['photoUpload']['name'];
echo "<br>The file type is: ". $_FILES['photoUpload']['type'];
echo "<br>The file size (bytes) is: ". $_FILES['photoUpload']['size'];
//check file size
if ( $_FILES['photoUpload']['size'] > 5242880 )
die ("<hr>Sorry, this file is larger than 5 MB");
echo "<hr>";
$allowedExts = array( "gif", "jpeg", "jpg", "png" );
$exp_array = explode(".", strtolower($_FILES['photoUpload']['name']));
$extension = end($exp_array);
$allowedType = array( "image/gif", "image/jpeg", "image/jpg", "image/png" );
if( !in_array($_FILES['photoUpload']['type'], $allowedType) )
die ("<br>Unsupported file type!");
if( !in_array($extension, $allowedExts) )
die ("<br>Unsupported file extension!");
// file_exists() - determine file has already been uploaded
$path = "C:/xampp/htdocs/PHP/". $_FILES['photoUpload']['name'];
if(!file_exists($path)){
echo "<hr>File does not exist. It is safe to move the temporary file<br>";
if( move_uploaded_file($_FILES['photoUpload']['tmp_name'], $path) ){
echo "The file was uploaded successfully.";
}
else{
echo "File upload failed!";
}
}
else{
echo "<br>File already exist. Please upload another file";
}
}
else{
echo "Please select the file to upload<br>";
echo "Error code: ". $_FILES['photoUpload']['error'];
}
?>