PHP and jQuery with AJAX upload not working

Jono

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>";
      }
   }
?>
Clarkie

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

jQuery AJAX file upload PHP

From Java

jQuery Ajax File Upload

From Dev

How to upload multiple files using PHP, jQuery and AJAX

From Dev

AJAX (jquery) to run PHP script not working

From Dev

Creating a smart remote upload script with jQuery AJAX and PHP

From Dev

Upload multiple image using AJAX, PHP and jQuery

From Dev

jQuery AJAX simple PHP Post not working

From Dev

Jquery, PHP and AJAX - why is this not working?

From Dev

Image Upload in PHP using AJAX Jquery not working

From Dev

Passing jquery ajax array to php not working

From Dev

Ajax Upload with JQuery and PHP

From Dev

upload file using ajax,jquery,php

From Dev

JQuery Ajax Upload of files

From Dev

How do that scripts jquery working for content upload grom ajax?

From Dev

How to upload multiple files using PHP, jQuery and AJAX

From Dev

AJAX (jquery) to run PHP script not working

From Dev

Creating a smart remote upload script with jQuery AJAX and PHP

From Dev

Upload files with Ajax in CodeIgniter with Jquery not working

From Dev

Large CSV file upload using JQuery/Ajax in php?

From Dev

jQuery $.ajax post to PHP in the same file not working

From Dev

upload a file in the same page as the form using php and ajax not working

From Dev

Ajax file upload with jquery not working

From Dev

PHP JQuery Ajax insert not working

From Dev

JQUERY + PHP + AJAX not working

From Dev

jQuery Ajax not working with $.ajax()

From Dev

Image Upload in PHP using AJAX Jquery not working

From Dev

AJAX call not working - PHP, MySQL, jQuery/Ajax

From Dev

JQuery if statement not working (AJAX/PHP)

From Dev

Upload file with AJAX not working