簡體   English   中英

PHP多圖像上傳,檢查大小和類型

[英]PHP multiple image upload with checking size and type

我設計了一個表單來使用PHP上傳多個圖像,我想檢查圖像的類型和大小。 但是我的代碼無法成功運行。 我該如何更正代碼?

HTML輸入文件(文件上傳的一部分):

<input type="file" class="test" name="file_array[]">
<input type="file" class="test" name="file_array[]">

PHP代碼

<?php
if (isset($_FILES['file_array'])) {
    $name_array     = $_FILES['file_array']['name'];
    $tmp_name_array = $_FILES['file_array']['tmp_name'];
    $type_array     = $_FILES['file_array']['type'];
    $size_array     = $_FILES['file_array']['size'];

    $allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
    $detectedType = exif_imagetype($tmp_name_array);

    $checktype = in_array($detectedType, $allowedTypes);
}

if ($checktype == false) {
    echo "type error";
    echo "</br>";
} elseif ($size_array > 2097152) {
    echo "type error";
    echo "</br>";
} else {
    for ($i = 0; $i < count($tmp_name_array); $i++) {
        if (move_uploaded_file(
            $tmp_name_array[$i], "upload/" . $name_array[$i]
        )
        ) {
            echo $name_array[$i] . " upload is complete<br>";
        } else {
            echo "function failed for " . $name_array[$i] . "<br>";
        }
    }
}
?>

您正在將數組傳遞給exif_imagetype() ,它應該是一個帶有文件名的字符串,對於您的示例,您應該遍歷您的數組或使用類似的東西:

// Get type from first image
$detectedTypeImage1 = exif_imagetype($tmp_name_array[0]);
$detectedTypeImage2 = exif_imagetype($tmp_name_array[1]);

然后你錯誤地將arrayint進行比較

$size_array > 2097152

你可以像我給的第一個例子那樣做,或者你可以在數組中使用循環,如:

foreach($size_array as $imageSize){
    if($imageSize > 2097152){
        echo "type error SIZE";
        echo "<br>";
    }
}

完整的例子

/**
    * Reorder the uploaded files array to simply the use of foreach
    * 
    * @param array $file_post
    * @return array
    */
    function reArrayFiles(&$file_post) {

    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}



// Array reArranged
$file_ary = reArrayFiles($_FILES['file_array']);

$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);


// For each file...
foreach($file_ary as $key => $file){

    static $i = 1;

    if($file['error'] == 4){
        echo 'No file uploaded on field '.$i++.'.<br>';
        continue;
    }

    $errors = false;

    // Check type, if not allowed tell's you wich one have the problem
    if(!in_array(exif_imagetype($file['tmp_name']), $allowedTypes)){
        echo '<span style="color:red">Filename '.$file['name'].' <b>type</b> is not allowed.</span><br>';
        $errors = true;
    }

    // Check size, if exceed allowed size tell's you wich one have the problem
    if($file['size'] > 2097152){
        echo '<span style="color:red">Filename '.$file['name'].' exceeds maximum allowed <b>size</b></span>.<br>';
        $errors = true;
    }


    // If we don't have errors let's upload files to the server
    if(!$errors){
        if (move_uploaded_file(
                $file['tmp_name'], "../upload/" . $file['name'])
        ){
            echo $file['name'] . " upload is complete<br>";

        }else{
            echo "Uploaded failed for " . $file['name'] . "<br>";
        }   
    }
    $i++;
}

不要忘記使用is_uploaded_file()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM