Can you upload images using Ajax?

KANAYO AUGUSTIN UG

I want to upload images using $.ajax, but I get the following PHP error:

undefined index:files

Here's my HTML and JS:

<form id="image_form" enctype="multipart/form-data">
    <input type="file" name="files[]" id="files[]" multiple >
    <input type="submit" name="submit" is="submit" />
</form>
<div id="result"></div>
<script src="js/jquery_library.js"></script>
<script>
    $(document).ready(function()
    {
        $('#image_form').submit(function(e)
        {
            e.preventDefault();             
            $.ajax({
                method: "POST",
                url: "upload.php",
                data: $(this).serialize(),
                success: function(status) 
                {
                    $('#result').append(status);
                }
            });
        });
    });
</script>

And here's my PHP:

<?php
include 'connect.php';
$allowed = array('jpg', 'png', 'jpeg', 'gif', 'bmp');
$myFile = $_FILES['files'];
$fileCount = count($myFile["name"]);

for ($i = 0; $i < $fileCount; $i++)
{
    $name = $myFile["name"][$i];
    $type = $myFile['type'][$i];
    $tmp_name = $myFile['tmp_name'][$i];
    $result = substr(sha1(mt_rand()),0,50);
    $explode = explode(".",$myFile["name"][$i]);
    $ext = end($explode);
    $target = "photos/".$result.".".$ext;

    if(in_array($ext, $allowed))
    {
        if(move_uploaded_file($tmp_name,$target))
        {
            mysqli_query($con, "INSERT INTO images VALUES('".$target."')");
            echo "<img src='$target' />";
        }
    }
} 
?>
KANAYO AUGUSTIN UG

I have figured it out, here is it:

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  

<script type="text/javascript">
        $(document).ready(function() {
            $('#image_form').submit(function(e) {
                e.preventDefault();  
               $.ajax({  
                    url: "upload.php",  
                    type: "POST",  
                    data: new FormData(this),  
                    contentType: false,  
                    processData:false,  
                    success: function(status) {
                        $('#result').append(status);
                    }
                });
            });
        });
</script>

//upload.php

<?php

include 'connect.php';

if(is_array($_FILES))   
 {  
      foreach ($_FILES['files']['name'] as $name => $value)  
      {  
           $file_name = explode(".", $_FILES['files']['name'][$name]);  
           $allowed_ext = array("jpg", "jpeg", "png", "gif");  
           if(in_array($file_name[1], $allowed_ext))  
           {  
                $new_name = substr(sha1(mt_rand()),0,50) . '.' . $file_name[1];  
                $sourcePath = $_FILES['files']['tmp_name'][$name];  
                $target = "photos/".$new_name;  
                if(move_uploaded_file($sourcePath, $target))  
                {  
                     mysqli_query($con, "INSERT INTO images VALUES('".$target."')");
                     echo "<img src='$target' />";
                }                 
           }            
      }   
 }  

?> 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

codeigniter form upload with images using ajax

From Dev

Android, can't upload images using WebView

From Dev

upload images using RESTAPI

From Dev

How to upload images via Ajax?

From Dev

Can you generate images and folders structures using Javascript

From Dev

Can't upload input into mysql database using ajax and php

From Dev

Can't upload input into mysql database using ajax and php

From Dev

Cannot upload images, but can upload pdf to assets

From Dev

Cannot upload images, but can upload pdf to assets

From Dev

Using Ajax to upload file

From Dev

Can you upload a file to the Slack API using files.upload as a different user?

From Dev

Asking user to upload images or using remote images

From Dev

Upload multiple images using AFNetworking

From Dev

Toggle images using ajax

From Dev

"You did not select a file to upload. " get this error while uploading image using ajax

From Dev

Ajax upload in CodeIgniter returns "you did not select a file to upload"

From Dev

Codeigniter 3 - Ajax File Upload (You did not select a file to upload)

From Dev

I can't upload my images into the website using android web view

From Dev

Upload file using WebApi, ajax

From Dev

WordPress image upload Using ajax?

From Dev

Upload image with phonegap using ajax

From Dev

Upload Image using ajax (json)

From Dev

Upload image to imgur using ajax

From Dev

Upload A File To Server Using AJAX

From Dev

Laravel update upload using AJAX

From Dev

"The maximum amount of data that you can upload is NaN undefined" error is coming on uploading an image using "Upload Media" button in Sitecore

From Dev

How can i upload images with NodeJS and express?

From Dev

Can't upload images to a public directory in laravel

From Dev

Can't upload images on Heroku server

Related Related

  1. 1

    codeigniter form upload with images using ajax

  2. 2

    Android, can't upload images using WebView

  3. 3

    upload images using RESTAPI

  4. 4

    How to upload images via Ajax?

  5. 5

    Can you generate images and folders structures using Javascript

  6. 6

    Can't upload input into mysql database using ajax and php

  7. 7

    Can't upload input into mysql database using ajax and php

  8. 8

    Cannot upload images, but can upload pdf to assets

  9. 9

    Cannot upload images, but can upload pdf to assets

  10. 10

    Using Ajax to upload file

  11. 11

    Can you upload a file to the Slack API using files.upload as a different user?

  12. 12

    Asking user to upload images or using remote images

  13. 13

    Upload multiple images using AFNetworking

  14. 14

    Toggle images using ajax

  15. 15

    "You did not select a file to upload. " get this error while uploading image using ajax

  16. 16

    Ajax upload in CodeIgniter returns "you did not select a file to upload"

  17. 17

    Codeigniter 3 - Ajax File Upload (You did not select a file to upload)

  18. 18

    I can't upload my images into the website using android web view

  19. 19

    Upload file using WebApi, ajax

  20. 20

    WordPress image upload Using ajax?

  21. 21

    Upload image with phonegap using ajax

  22. 22

    Upload Image using ajax (json)

  23. 23

    Upload image to imgur using ajax

  24. 24

    Upload A File To Server Using AJAX

  25. 25

    Laravel update upload using AJAX

  26. 26

    "The maximum amount of data that you can upload is NaN undefined" error is coming on uploading an image using "Upload Media" button in Sitecore

  27. 27

    How can i upload images with NodeJS and express?

  28. 28

    Can't upload images to a public directory in laravel

  29. 29

    Can't upload images on Heroku server

HotTag

Archive