I have a js page that uses AJAX to take an image supplied in a <input type="file" />
, send it through to a php page (upload.php) through POST. The jQuery runs as expected, the code for when it's successful runs, I know because I get the alert("Success");
. Therefore I believe the problem lies in my PHP page. The files are displayed below. Thanks in advance.
HTML:
<form id='uploadimage' method='post' enctype='multipart/form-data'>
<div id='selectImage'>
<input type='file' name='file' id='file' required />
<input type='button' id='imageUpload' value='Upload' class='submit' />
</div>
</form>
jQuery:
$(document).ready(function (e) {
$("#imageUpload").on('click',(function(e) {
e.preventDefault();
$('#loading').show();
$.ajax({
url: "upload.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function() { // A function to be called if request succeeds
alert("success");
$('#loading').hide();
}
});
}));
});
PHP:
<?php
if(isset($_FILES["file"]["type"])) {
$validextensions = array("jpeg", "jpg", "png");
$temporary = explode(".", $_FILES["file"]["name"]);
$file_extension = end($temporary);
if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")
) && ($_FILES["file"]["size"] < 100000000)
&& in_array($file_extension, $validextensions)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>";
} else {
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " <span id='invalid'><b>already exists.</b></span> ";
} else {
$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
$targetPath = "upload/".$_FILES['file']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
echo "<span id='success'>Image Uploaded Successfully...!!</span><br/>";
echo "<br/><b>File Name:</b> " . $_FILES["file"]["name"] . "<br>";
echo "<b>Type:</b> " . $_FILES["file"]["type"] . "<br>";
echo "<b>Size:</b> " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "<b>Temp file:</b> " . $_FILES["file"]["tmp_name"] . "<br>";
}
}
}
else {
echo "<span id='invalid'>***Invalid file Size or Type***<span>";
}
}
?>
It looks like you're not sending the file (or any other form data) with the $.ajax().
You can use the FormData object which lets you compile a set of key/value pairs (including File input) to send with ajax. The transmitted data is in the same format that the form’s submit method would use to send the data if the form’s encoding type were set to multipart/form-data. https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments