简体   繁体   中英

Error in my php file I can't seem to find? Perhaps caused by ajax?

First of all, I've just met with ajax and jquery and I must admit they seem pretty interesting. But I lost quite some time on figuring out why are my results in uploading img-s always the same.The idea is creating a page where I could import multiple images with some restrictions such as size and extension,but for some reason errors just aren't printing. It just prints alert("Image Uploaded") no matter what the result. This is the ajax part of my html:

<script>  
 $(document).ready(function(){  
      $('#uploadForm').on('submit', function(e){  
           e.preventDefault();  
           $.ajax({  
                url: "upload.php",  
                type: "POST",  
                data: new FormData(this),  
                contentType: false,  
                processData:false,  
                success: function(data)  
                {  
                     $("#gallery").html(data);  
                     alert("Image Uploaded");  
                }  
           });  
      });  
 });  

 </script>

And this is the upload.php that I simply call in my html file:

 <?php  
 //upload.php  
 $output = '';  
 if(is_array($_FILES))   
 {  
      foreach ($_FILES['files']['name'] as $name => $value)  
      {  
      $totalImageIndex = ($name+1);
           $file_name = explode(".", $_FILES['files']['name'][$name]);  
           $file_size = $_FILES['files']['size'][$name];
           $allowed_ext = array("png", "gif");  
           if(in_array($file_name[1], $allowed_ext))  
           {  
            if($totalImageIndex <= 5 ) {
                // 2 MB is 2097152 bytes.
                if($file_size < 2097152){
                $new_name = $totalImageIndex . '.' . $file_name[1];  
                $sourcePath = $_FILES['files']['tmp_name'][$name]; 
                $targetPath = "slike/".$new_name;  
                if(move_uploaded_file($sourcePath, $targetPath)) 

                {  
                     $output .= '<img src="'.$targetPath.'" width="150px" height="180px" />';  
                }

                }   
else { continue ; }             
           }    else echo 'file is too big!';       
      } else   echo 'wrong file format!';     
 }
 echo $output;}

 ?>  

Any idea or suggestion would be appriciated, thank u in advance!

In ajax on success you will get the data back that you echoed on your php file so either you get the image back or the error back you can simply alert(data); only and see what you getting in your ajax code you are alerting alert(image uploded) which will always be called as you are getting an error data also as success just remove that alert and do only alert(data) and you will see the error if any

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM