Reputation: 1435
In my index.php
I have a form where a file upload will happen
<form id="uploadImage" method="post" action="upload.php">
<span class="input-group-text">
<label for="attach-doc" class="attachment-icon mb-0">
<i data-feather="image" class="cursor-pointer lighten-2 text-secondary"></i>
<input type="file" name="uploadFile" id="uploadFile" accept=".jpg, .png" /> </label></span>
</form>
I have a jquery for ajax submit as
$('#uploadFile').on('change', function(){
$('#uploadImage').ajaxSubmit({
// target: "#editor1 .ql-editor",
// resetForm: true
});
});
And my upload.php
code is
<?php
//upload.php
if(!empty($_FILES))
{
if(is_uploaded_file($_FILES['uploadFile']['tmp_name']))
{
$ext = pathinfo($_FILES['uploadFile']['name'], PATHINFO_EXTENSION);
$allow_ext = array('jpg', 'png');
if(in_array($ext, $allow_ext))
{
$_source_path = $_FILES['uploadFile']['tmp_name'];
$target_path = 'upload/' . $_FILES['uploadFile']['name'];
if(move_uploaded_file($_source_path, $target_path))
{
echo '<p><img src="'.$target_path.'" class="img-thumbnail" width="200" height="160" /></p><br />';
}
//echo $ext;
}
}
}
?>
So as per the code when I click on the upload file
a ajax call should happen and the image should be uploaded to the folder upload .
But upload is not happening with the above code, Any help appreciated.
Upvotes: 0
Views: 85
Reputation: 2137
You have missed one form attribute enctype="multipart/form-data"
that is used for file uploading.
If the form tag does not have this attribute, then your file will not be uploaded to the server.
Please change following line:
<form id="uploadImage" method="post" action="upload.php">
to
<form id="uploadImage" method="post" action="upload.php" enctype="multipart/form-data">
I hope this will work for you
Upvotes: 1