Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
imageUpload.html

XML
<html>
<head>
<script src="../js/jquery-1.11.1.min.js"></script>

</head>
<body>
<div id="project_features" class="project_features">
        <form action="uploader.php" enctype="multipart/form-data">
                <label for="file">Featured Image:</label>
                <input type="file" name="myfile[]" multiple="multiple" id="featured_image"><br>

        <input type="submit" id="submit" value="Submit" />
                     </form>
    </div>
</body>
</html>



uploader.php

PHP
<?php


$upload_dir = "uploaded_files/images/";

if (isset($_FILES["myfile"])) {
    $no_files = count($_FILES["myfile"]['name']);
    for ($i = 0; $i < $no_files; $i++) {

        if ($_FILES["myfile"]["error"][$i] > 0) {
            echo "Error: " . $_FILES["myfile"]["error"][$i] . "<br>";
        } else {
            $temp = explode(".", $_FILES["myfile"]["name"][$i]);
            $extension = end($temp);
                 move_uploaded_file($_FILES["myfile"]["tmp_name"][$i], $upload_dir . $_FILES["myfile"]["name"][$i]);
                echo "<br><font color='green'>".$_FILES["myfile"]["name"][$i] . " Uploaded Successfully.</font><br>";
        }
    }
}
?>



This code is storing the multiple Images in required folder.But How can I do the same functionality using jQuery and Ajax?Please help me sort this one out!
Posted

1 solution

here's a nice and small jquery plugin for that https://github.com/jfeldstein/jQuery.AjaxFileUpload.js[^]
 
Share this answer
 
Comments
Janardhanam Julapalli 17-Nov-14 7:27am    
I should not use any plugins.I should do it by ajax.
Adrian Fatol 17-Nov-14 7:42am    
there are many solutions to this, see: https://www.google.ro/search?q=upload+image+using+ajax+jquery&oq=upload+image+using+ajax. what kind of problem do you encounter, more precisely?
Janardhanam Julapalli 17-Nov-14 8:27am    
<script>
jQuery(document).ready(function()
{
jQuery('#submit').click(function()
{
var featured_image = jQuery('#featured_image')[0].files;
var no_of_files = jQuery('#featured_image')[0].files.length;

var form_data = new FormData();
form_data.append('myfile', featured_image)
$.ajax(
{
url: 'uploader.php', // point to server-side PHP script
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
type: 'post',
success: function(php_script_response)
{
alert(php_script_response); //display response from the PHP script, if any
}
});
});
});
</script>
Janardhanam Julapalli 17-Nov-14 8:29am    
I am using above ajax code to send the images to php but I know that it will not work.I don,t know how to proceed.Just I need to store the selected image files to be stored in a folder via AJAX.
Adrian Fatol 18-Nov-14 3:49am    
- first you need to suppress the events when clicking the submit button:

jQuery('#submit').click(function(event)
{
event.stopPropagation(); // Stop stuff happening
event.preventDefault(); // Totally stop stuff happening

- second, you need to add the data to your ajax post:

$.ajax(
{
url: 'uploader.php', // point to server-side PHP script
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
type: 'post',
data: form_data
..................


posting the html would also help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900