Uploading images with the help of arrays and fetch errors

五迷三道 提交于 2019-12-25 03:25:11

问题


I use a script to upload a couple of images to a directory the code works great in case of just one picture will be uploaded.

If I want to upload two images or more and have an extension that is not accepted, the script will upload the one with the extension that is allowed to upload and shows the error message for the one who is not accepted, but the upload takes place. that's my first problem.

Second problem: In case of an error message I would like to display a message in which it is said, which of the images will be not allowed. I do not know how to fetch this one that has an unaccepted ending into a variable that I can echo to the error message.

Here is the code I use:

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

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

    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 = strtolower(end(explode('.', $file_name)));
        if (in_array($file_extension, $allowed_extension) === false){
            $errors[] = 'its an unaccepted format in picture $variable_that_count';
            continue;
        }

        if ($file_size > 2097152){
            $errors[] = 'reached maxsize of 2MB per file in picture $variable_that_count';
        }

        if (count($errors) == 0){
            $path = "a/b/c/";
            $uploadfile = $path."/".basename($_FILES['image']['name'][$key]);

            if (move_uploaded_file($_FILES['image']['tmp_name'][$key], $uploadfile)){
                echo "success.";
            }
        }
    }
}
?>

Hope it will be clear what I like to approach. If there is someone who could help out I really would appreciate. Thanks a lot.


回答1:


This would work for you

Sample HTML

<form action="" enctype="multipart/form-data" method="post">

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <div>
        <input type="submit" value="Send">
    </div>
</form>

PHP Code

$allowedExtention = 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 ) {

        $fileName = $_FILES ['image'] ['name'] [$key];
        $fileSize = $_FILES ['image'] ['size'] [$key];
        $fileTemp = $_FILES ['image'] ['tmp_name'] [$key];

        $fileExtention = pathinfo ( $fileName, PATHINFO_EXTENSION );
        $fileExtention = strtolower ( $fileExtention );

        if (! in_array ( $fileExtention, $allowedExtention )) {
            $errors [$fileName] [] = "File format $fileExtention not accepted for $fileName";
            continue;
        }

        if ($fileSize > 2097152) {
            $errors [$fileName] [] = 'reached maxsize of 2MB per file in picture $variable_that_count';
continue ;
        }

        if (count ( $errors ) == 0) {
            $path = "temp";
            $prifix = basename ( $fileName, "." . $fileExtention );

            var_dump ( $prifix );

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

            if (move_uploaded_file ( $fileTemp, $uploadfile )) {
                $fileName = basename ( $uploadfile );
                $output [$fileName] = "OK";
            } else {
                $output [$fileName] = "ERORR";
                $errors [$fileName] [] = "Can Move uploaded file to destination";
            }
        }
    }
}

var_dump ( $errors );
var_dump ( $output );

Sample Output

string '79534296' (length=8)
string '89773706' (length=8)
array
  'download (1)' => 
    array
      0 => string 'File format  not accepted for download (1)' (length=42)
  'brief.docx' => 
    array
      0 => string 'File format docx not accepted for brief.docx' (length=44)
  '' => 
    array
      0 => string 'File format  not accepted for ' (length=30)
array
  '79534296-2.jpg' => string 'OK' (length=2)
  '89773706-2.jpg' => string 'OK' (length=2)

Edit 1

if all the files must be valid there are 2 ways to achieve that

A. Start By validating all files first ;

foreach ( $_FILES ['image'] ['name'] as $key => $array_value ) {
    if(! in_array (pathinfo ($_FILES ['image'] ['name'] [$key], PATHINFO_EXTENSION ), $allowedExtention ))
    {
        die("Die! Die! Die") ;
    }
}

foreach ( $_FILES ['image'] ['name'] as $key => $array_value ) {
  // Upload Script here 
 }

B. Remove all files if an error is detected

foreach ( $_FILES ['image'] ['name'] as $key => $array_value ) {
  // Upload Script here 
 }

// Remove All Files
if(count($errors) > 0)
{
    foreach ($output as $key => $value)
    {
        @unlink($path . "/" . $key);
    }

    die("Die! die! die!") ;
}

I hope this helps



来源:https://stackoverflow.com/questions/10093366/uploading-images-with-the-help-of-arrays-and-fetch-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!