1

i modified my upload code based on this post https://stackoverflow.com/a/30074716/14787718 (its working code)

but my code does not work, please help.

my full code here: https://sandbox.onlinephpfunctions.com/code/c62b6bdf6fadd5aff63b2e7e65e75c1075d1dbb0

<?php
if (isset($_POST['upload']) && $_FILES['image']['error']==0) {
$j = 0; //Variable for indexing uploaded image 

for ($i = 0; $i < count($_FILES['image']['name']); $i++) {//loop to get individual element from the array

    $allow_ext = array('png','jpg','gif','jpeg','bmp','tif');
    $allow_type = array('image/png','image/gif','image/jpeg','image/bmp','image/tiff');

    $image_name = $_FILES['image']['name'][$i];
    $image_type = getimagesize($_FILES['image']['tmp_name'][$i]);
    $image_name = explode('.',$image_name);
    $ext = end($image_name);
    $j = $j + 1;//increment the number of uploaded images according to the files in array       

    if(in_array($ext, $allow_ext) && in_array($image_type['mime'], $allow_type)){
        list($width, $height, $mime) = getimagesize($_FILES['image']['tmp_name'][$i]);
        if ($width>0 && $height>0) {
            $upload = move_uploaded_file($_FILES['image']['tmp_name'][$i], "uploads/".$_FILES['image']['name'][$i]);
            if ($upload) {
                echo '<p>File Uploaded: <a href="uploads/'.$_FILES['image']['name'][$i].'">View Image</a></p>';
            }
        } else {
        echo 'Error: Only image is allowed!';
        }
    } else {
        echo 'Error: Invalid File Type!';
    }
 }
}
?>

thank you!

Manh
  • 123
  • 11

1 Answers1

0

first move the Jquery call to place it before bootstrap:

<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.0/js/bootstrap.min.js'></script>

And at your first test : if (isset($_POST['upload']) && $_FILES['image']['error']==0). $_FILES['image']['error'] is an array.

Delete this test to keep only if ( isset($_POST['upload']) )

Add this test in you for step:

for ($i = 0; $i < count($_FILES['image']['name']); $i++) {//loop to get individual element from the array

        if ($_FILES['image']['error'][$i]==0) {

Tested, it works fine!

Monnomcjo
  • 715
  • 1
  • 4
  • 14