2

i have 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'])... 

and my problem is, that i dont know how to display the error messages that should be shown, when the array

$errors

is given. up to now, it just will be displayed:

array

instead of:

maxsize of 2MB on abc.jpg has reached

in the html i have this code:

<?php if(isset($errors)):?> 

    <div class="form-error-message" id="signup-error-message" style="display": none;">
        <div class="arrow-wrapper">
            <div class="border-wrapper">
            <?php foreach($errors as $error):?>
                <p class="layer-content">
                <?php echo $error;?>
                </p>
            <?php endforeach;?>
            </div>
        </div>

    </div>

if there is someone who could be that friendly and help me out i really would appreciate. thanks a lot.

1
  • Why can't you use print_r to display error instead of echo it. Commented Apr 11, 2012 at 18:25

5 Answers 5

11

Maybe save each error like this:

$errors = array();
$errors[] = $file_name . ": this is the error message.";

And display like this:

if(count($errors) > 0){
    foreach($errors as $e){
        echo $e . "<br />";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

You've wrote $errors as 2-dimensional array:

$errors[$file_name][] = 'your message';

So to correctly display it use two foreach's instead of one:

        <?php foreach($errors as $errorsOnFile):?>
            <?php foreach($errorsOnFile as $error):?>
                <p class="layer-content">
                <?php echo $error;?>
                </p>
            <?php endforeach;?>
        <?php endforeach;?>

Comments

3

How about?

echo implode(', ', $errors[$filename]);

Comments

3

You have to print_r an array, not echo it:

print_r($errors);

Alternatively, you can show each of the errors individually, like so:

foreach( $errors as $innerErrors )
{
  foreach( $innerErrors as $anError )
  {
    echo $anError ."\n";
  }
}

7 Comments

he could echo it, it's in loop, but not selected index in array
you mean i simply could add: if (count($errors) > 0){foreach($errors AS $error){print_r($error) . "<br />";}} ? that doesnt work. it also will display just "array". thanks.
@bonny: That's because $errors is apparently an array of arrays. Do the same think for each of the inner arrays.
i inserted that between "if ($file_size > 2097152){...}" but it doesnt work. now it doesnt display "array" like before. there is nothing now.
@bonny: You'll need to add thinks like bound checking, to make sure the arrays are populated before you loop over each, and so on.
|
2

You can't just echo an array. You can, however, loops through the elements of the array.

foreach($errors[file_name] as $error_message) {
    echo $error_message."<br>";
}

This code echos the errors one at a time.

Comments

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.