0

I need to upload several images, I have 2 problems, the first one that does not allow me to upload 2 or more files when it is from the cell phone (here something important, if it is from the cell phone you must open the camera and if it is in PC it must show the window of file selection, this works fine, but with the cell phone it only leaves one, so far I have only tried it on Android using Crhome) and the second detail is with the first element is not saved and if it is just a file because it does not either, it seems that it does not take position [0], when I put more than one image, the first does not save and the others are saved correctly. I've been trying for a while and I do not see the problem. Annex the structure of my files: \camera
└───uploads
└───index.php
└───upload.php

index.php :

<html>
<head>
    <meta charset="UTF-8">
    <title>upload</title>
</head>
<body>
    <form action="upload.php" method="post" multipart="" enctype="multipart/form-data">
        <input type="file" name="img[]" accept="image/*" id="capture" capture="camera" multiple >
        <input type="submit">
    </form>
</body>
</html>

And upload.php :

<?php
            echo '<pre>';
            $img = $_FILES['img'];

            if(!empty($img))
            {
                $img_desc = reArrayFiles($img);
                print_r($img_desc);

                foreach($img_desc as $val)
                {
                    $newname = date('YmdHis',time()).mt_rand().'.jpg';
                    move_uploaded_file($val['tmp_name'],'./uploads/'.$newname);
                }
            }

            function reArrayFiles($file)
            {
                $file_ary = array();
                $file_count = count($file['name']);
                $file_key = array_keys($file);

                for($i=0;$i<$file_count;$i++)
                {
                    foreach($file_key as $val)
                    {
                        $file_ary[$i][$val] = $file[$val][$i];
                    }
                }
                return $file_ary;
            }
        ?>
Alphalapz
  • 1
  • 2

1 Answers1

0

This works for me, Hop this will solve your second problem.

if (isset($_FILES['Gallery']) && is_array($_FILES['Gallery'])) {
                      $errors= array();
                      foreach($_FILES['Gallery']['tmp_name'] as $key => $tmp_name ) {
                        $file_name = $key.$_FILES['Gallery']['name'][$key];
                        $file_size =$_FILES['Gallery']['size'][$key];
                        $file_tmp =$_FILES['Gallery']['tmp_name'][$key];
                        $file_type=$_FILES['Gallery']['type'][$key];
                        if($file_size > 2097152){
                          $errors[]='File size must be less than 2 MB';
                        }
                        if (empty($errors)==true) {
                          if (is_dir('uploads')==false) {
                            mkdir('uploads', 0700);     // Create directory if it does not exist
                          }

                          if (file_exists("uploads/".$file_name)==false) {
                            move_uploaded_file($file_tmp,"uploads/".$file_name);
                            chmod("uploads/".$filename, 0777);
                            $Gallery_Link = "uploads/".$file_name;
                          } else {                                  // rename the file if another one exist
                            $Gallery_Link = "uploads/".time()."_".$file_name;
                            rename($file_tmp,$Gallery_Link) ;
                          }

                        } else {
                          echo $errors;
                        }
                      }
                    }
Joy Kalyan
  • 13
  • 7
  • Your `reArrayFiles` looks wrong. `count($file['name'])` won't tell you how many files were uploaded. – chiliNUT Nov 22 '17 at 23:17
  • same problem, the first image had a mistake u.u – Alphalapz Nov 23 '17 at 15:44
  • Result of your intact code, it works fine here. `Array ( [0] => Array ( [name] => 25.jpg [type] => image/jpeg [tmp_name] => /tmp/phpxGVpJt [error] => 0 [size] => 94727 ) [1] => Array ( [name] => 26.jpg [type] => image/jpeg [tmp_name] => /tmp/phpH4oPuq [error] => 0 [size] => 73318 ) )` – Joy Kalyan Nov 23 '17 at 16:11