0

i have a script to upload a couple of images to a directory. i realize that with the help of arrays. therefore i got this code:

$allowed_extension = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif');
$errors = array();
$output = array();

if(!empty($_FILES['image']['tmp_name'])){  

     foreach($_FILES['image']['name'] as $key => $array_value){

        if(!in_array(pathinfo($_FILES['image']['name'][$key], PATHINFO_EXTENSION), $allowed_extension)){
                die("Die!");
        }
    }

    foreach($_FILES['image']['name'] as $key => $array_value){

       $file_name = $_FILES['image']['name'][$key];
       $file_size = $_FILES['image']['size'][$key];
       $file_tmp = $_FILES['image']['tmp_name'][$key];

       $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
       $file_extension = strtolower($file_extension);

         if (!in_array($file_extension, $allowed_extension)){
        $errors[$file_name][] = "format $file_extension in image $file_name is not accepted";
        continue;
         }

         if ($file_size > 2097152){
        $errors[$file_name][] = "maxsize of 2MB on $file_name has reached";
                    }

         if (count($errors) == 0){

        $dir = "a/b/c";

        if (is_dir($dir)){
            mkdir("a/b/c/tmp_images", 0755);
        }else{
            mkdir("a/b/c", 0755);
            mkdir("a/b/c/tmp_images", 0755);
        }

        $path = "a/b/c/tmp_images"; 
        $prifix = basename($file_name, "." . $file_extension);

        //var_dump ($prifix);

        $uploadfile = $path . "/" . $file_name;

        $x = 0;
        while (file_exists($uploadfile)){
               $x ++;
               $uploadfile = "{$path}/{$prifix}-{$x}.{$file_extension}";
            }

            if (move_uploaded_file($file_tmp, $uploadfile)){
               $file_name = basename($uploadfile);
               $output [$file_name] = "OK";

            }else{

            $output[$file_name] = "Failure while Uploading!";
            $errors[$file_name][] = "Failure: Can't move uploaded pictures!";
            }//else...
         }//if(count($errors))...
    }//foreach($_FILES['image']['name']... 
}//if(!empty($_FILES['image']['tmp_name'])... 

now my first problem is: when i have two different kind of file types, the uploading will be aborted and the error message "Die!" will be displayed. as it can be seen in the code the first foreach-part will do that. the second foreach-part is doing what i actually like to display but when i leave the first foreach-part in case of one wrong and one right filetype the upload will take place. the problem with that is, that an user can't make any changes after submitting the data. therfor the upload has to be aborted.

my second problem is: when i leave the first foreach-part it will be displayed just "array" instead of showing the text of array [0] which contains the error message in case of an wrong filetype or size. so the output should not be "array" it should be "maxsize of 2MB on $file_name has reached"

so i have no clue how to solve that. i really would appreciate if there is someone who could tell me what to do. thanks a lot.

4
  • Why are you looping through a specific key in the $_FILES superglobal? $_FILES['image'] references one of the images. No two images can have the same key. This will only ever loop once... Can you provide the HTML? Commented Apr 11, 2012 at 16:22
  • Bonny - one suggestion. When you're dealing with arrays, you can use the print_r function to see the structure of the array. So if there is a part of your code where you can't figure out how to handle an array, use print_r to output what's going on under the hood. Most of the time, this will show you where you're making assumptions about the structure of the array that may be unwarranted. Commented Apr 11, 2012 at 16:34
  • Use if ($_FILES['image']['error'] === UPLOAD_ERR_OK) to check for upload success. You may still get a tmp_name even though the file's truncated/corrupted. and don't trust the user-provided filename. It's beyond trivial to do ren nastyvirus.exe cutekittens.jpg and slip right through your 'security' system. Commented Apr 11, 2012 at 17:14
  • hello and thanks. but what has that to do with my question? that doesnt solve my problems. Commented Apr 11, 2012 at 17:38

1 Answer 1

1

Do not use die("Die!"); in your code as it terminates execution of the script. Provide your HTML to find the route cause.

Sign up to request clarification or add additional context in comments.

2 Comments

what does that mean -> Provide your HTML to find the route cause? thanks.
The code where you have kept file selection and browse button for multiple files...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.