【问题标题】:PHP multiple image upload with checking size and typePHP多张图片上传,检查大小和类型
【发布时间】:2015-02-20 14:00:26
【问题描述】:

我设计了一个表单来使用 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>";
        }
    }
}
?>

【问题讨论】:

  • 您应该将名称 file_array[] 设置为 file_array - 因为名称实际上不是一个数组,它是对发布到脚本的对象的引用。

标签: php


【解决方案1】:

您将一个数组传递给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()

【讨论】:

  • 我试试你的代码。但仍然无法检查图像类型。
  • 我已经编辑了我的答案,您只需要遵循逻辑即可。再次检查我的答案。
  • 这个例子有一些问题,如果我只上传一张图片,你的例子就不能工作。另外,如果我上传两个文件,file1 的文件类型有效 n file2 的类型无效,代码无法进行验证,两个文件都无法上传到服务器。我该如何解决这两个问题
  • 我试图向你解释你是否能够达到你的目标,你应该明白做了什么:) 查看我的编辑答案。
  • 您的新答案的问题是,如果我只在表单中上传一个文件,代码仍然会验证两个输入,因为我的表单有 2 个空间供我上传文件。如果我只上传一个文件,我会看到(警告:exif_imagetype(): Filename cannot be empty in C:\wamp\www\Paul\uplaoditem\uploadc.php on line 37)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2011-08-26
  • 2017-08-20
  • 2014-04-29
相关资源
最近更新 更多