fortrustit
fortrustit

Reputation: 91

Show error (array) feedback in the page with php

I would like to show users upload status with uploaded and failed files. Here's my code for the multiple files upload


<form  method="post" enctype="multipart/form-data" > 
        <?php 
        //codes to echo error and uploaded file status
        ?>
        <input type="file" name="files[]" multiple>
        <input type="submit" value="Upload Images" name="submit_image">
</form>

My code to validate the uploaded images is:

<?php 
 if (isset($_POST["submit_image"])) {
    if (!empty($_FILES["files"]["name"][0])) {
        $files = $_FILES["files"];
        //arrarys to include files uploaded successfully and failed
        $uploaded = array();
        $failed = array();
        //allowed extensions of the files
        $allowed = array("jpg","gif","jpeg","png");
        //access tmp_name arrary
        foreach ($files['name'] as $position => $file_name) {
            $file_tmp = $files["tmp_name"][$position];
            $file_size = $files["size"][$position];
            $file_error = $files["error"][$position];
            $file_ext = explode(".", $file_name);
            $file_ext = strtolower(end($file_ext));
            if (in_array($file_ext, $allowed)) {
                if ($file_error === 0) {
                    if ($file_size  <= 204800) {
                        //move the valid file to the upload folder
                        $file_name_new = uniqid("", true) . "." . $file_ext;
                        $file_destination = "uploads/" . $file_name_new;
                        if (move_uploaded_file($file_tmp, $file_destination)) {
                            $uploaded[$position] = $file_destination;

                        } else {
                            $failed[$position] = "[{$file_name}] failed to upload.";
                        }
                    } else {
                        $failed [$position] = "[{$file_name}] is larger than 200kb.";

                    }
                } else {
                    $failed[$position] = "[{$file_name}] failed to upload.";

                }
            } else {
                $failed[$position] = "[{$file_name}] file extension {$file_ext} is not allowed. Only jpg, jpep, png and gif are accetable.";  
            }
        }
}?>

I don't want to print out anything containing array or things users don't understand. What's the best way to do it? Thanks!

Upvotes: 0

Views: 56

Answers (1)

fortrustit
fortrustit

Reputation: 91

<?php if($uploaded || $failed){ ?>
            <ul>
                <?php foreach($uploaded as $msg){
                    echo "<li>".$msg."</li>";
                }?>
            </ul>
            <ul>
                <?php foreach($failed as $msg){
                    echo "<li>".$msg."</li>";
                }?>
            </ul>  
<?php } ?>

This will show the error message and succefully uploaded message in a list. Also, this might be helpful.emb HTML in php

Upvotes: 1

Related Questions