Upload images with AJAX ERORR

0

Hello, good morning, people have a problem with the ajax code to upload images but I do not know where this error is, PHP is fine because I tried it so you can tell me where you are.

index.php with the ajax code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
function upload_img(){

    var formData = new FormData($("#formUpload")[0]);
    $.ajax({
    type: 'POST',
    url: 'upload.php',
    data: formData,
    contentType: false,
    processData: false
    });

}
</script>

<form onsubmit="return false" class="oculto" method="post" enctype="multipart/form-data" id="formUpload">
    <input type="file" name="photoimg" onchange="upload_img();">
</form>

upload.php the code to upload:

<?php
include('db.php');
include('alphaID.php');
session_start();
$session_id='1'; //$user id
$path = "uploads/";
//$uid = "uploads/";


$valid_formats = array("png", "PNG");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
   $name = $_FILES['photoimg']['name'];
   $type = $_FILES['photoimg']['type'];
   $size = $_FILES['photoimg']['size'];
   if(strlen($name)) {
       $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION)); 
       $name = alphaID(microtime(true) * 10000);
       if(in_array($ext,$valid_formats)) {
       if($size<(50024*50024)) {
          $actual_image_name = $name;
          $video_ext=$ext;
           $tmp = $_FILES['photoimg']['tmp_name'];
           if(move_uploaded_file($tmp, $path.$actual_image_name.'.'.$video_ext)) {
              shell_exec("ffmpeg -i uploads/".$actual_image_name.".flv -f flv -s 650x390 uploads/".$actual_image_name.".mp4");
              shell_exec("ffmpeg -i uploads/".$actual_image_name.".mp4 -vcodec png -ss 00:00:15 -s 650x390 -vframes 1 -an -f rawvideo uploads/".$actual_image_name.".png");

              $newdata=$actual_image_name;
              if($newdata) {
                 echo '<img src="uploads/'.$actual_image_name.'.png" width="100%" height="auto" class="preview_img" />';
              }
            } else {
                echo "Fail upload folder with read access.";
            }
         } else
            echo "Image file size max 1 MB";                    
         } else
            echo "Invalid file format.";    
     } else
        echo "Please select image..!";
     exit;
  }
?>
    
asked by Shareiv 15.03.2017 в 18:06
source

1 answer

0

I get the impression that attaching the upload_img function to the change event of the input file is not the correct way. I would do it like this:

<form onsubmit="upload_img();" class="oculto" method="post" enctype="multipart/form-data" id="formUpload">
    <input type="file" name="photoimg" >
    <input type="submit">
</form>

And I would worry about returning false to the end of upload_img .

    
answered by 15.03.2017 в 18:54