My last question was related to a bad reloading of a PHP page after uploading a file.
Now I have to reload just a div instead of the entire page, and JQuery is not my best friend..
I put the form into a div as follows, with the following JQuery function (I got the JQuery code from here):
<div id="updiv">
<form id="uploadFile" action="" method="post" enctype="multipart/form-data">
<h3>Upload a file:</h3>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" id="submit" value="Upload" name="submit">
</form>
</div>
<script>
$('#uploadFile').submit(function()
{
event.preventDefault();
$.ajax(
{
url: "upload.php",
type: 'POST',
data: $(this).serialize(),
success: function(data)
{
$("#updiv").fadeOut();
setTimeout(1000);
$("#updiv").fadeIn();
});
});
$(this).ajaxSubmit(options);
return false;
});
</script>
And this is the upload.php file:
<?php
/* upload.php */
set_time_limit(0);
$targetDir = "/path/to/upload/dir";
$targetFile = $targetDir . basename($_FILES["fileToUpload"]["name"]);
$upload = 1;
$fileType = pathinfo($targetFile, PATHINFO_EXTENSION);
if(isset($_POST["submit"]))
{
/* Check file size */
if($_FILES["fileToUpload"]["size"] > 500000)
{
echo "Sorry, your file is too large.";
$upload = 0;
}
/* Allow certain file formats */
if($fileType != "data" )
{
echo "Sorry, non valid filetype.";
$upload = 0;
}
/* Check if $uploadOk is set to 0 by an error */
if($upload == 0)
{
echo "Sorry, your file was not uploaded.";
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile))
{
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
else
{
echo "Sorry, there was an error uploading your file.";
}
}
}
If I change the form's action attribute as:
<form id="uploadFile" action="upload.php" method="post" enctype="multipart/form-data">
<h3>Upload a file:</h3>
<input type="file" name="fileToUpload" id="fileToUpload" required>
<input type="submit" id="submit" value="Upload" name="submit">
</form>
I can upload the file but nothing is reloaded as I want, and I think it's obvious..
Otherwise, with the code posted above, the browser returns the error:
501 Not Implemented
The requested method is not recognized
From here I see that this is a web server problem.. The web server is httpd.
How can I solve my problem ? Thank you in advance.