0

我有许多需要验证的上传文件表单(缩短的示例):

<form>
<input type='file' name='file[]' class='file_upload_button'>
<input type='file' name='file[]' class='file_upload_button'>
<input type='file' name='file[]' class='file_upload_button'>
<input type='file' name='file[]' class='file_upload_button'>

<input type='submit' value='Save Draft' class='save_draft_button'>
</form>

而且我希望每个上传输入都通过文件类型进行验证,但我很难理解需要做什么。这是我正在尝试的那种事情,但显然它是不对的!

if (! empty($_FILES['file']['name'][0])) {  
// VALIDATION goes here
    }

但我不知道如何选择,例如,第一个上传字段 - 我尝试使用 $_FILES['file']['name'][0] 但无济于事。任何 hep 将不胜感激!

4

1 回答 1

2

一个偶然的机会,我昨天写了以下脚本。
这是用于调整图像、PNG 或 GIF 或 JPEG 的大小。
这需要 './tmp' 目录。
如果你喜欢,请参考这个。

<?php

$html = PHP_EOL;

if (!empty($_FILES['images'])) {

    $finfo = new finfo(FILEINFO_MIME);

    for ($i=0;;$i++) {

        switch (true) {      
            case (!isset($_FILES['images']['tmp_name'][$i])):
                break 2;
            case (!is_uploaded_file($filename = $_FILES['images']['tmp_name'][$i])):
            case (($type = $finfo->file($filename)) === false):
                continue 2;
            case ($type === 'image/png; charset=binary'):
                $img = imagecreatefrompng($filename);
                break;
            case ($type === 'image/jpeg; charset=binary'):
                $img = imagecreatefromjpeg($filename);
                break;
            case ($type === 'image/gif; charset=binary'):
                $img = imagecreatefromgif($filename);
                break;
            default:
                continue 2;
        }

        list($width, $height) = getimagesize($filename);
        $new_width  = 100;
        $new_height = (int)($new_width * $height / $width);
        $new_img    = imagecreatetruecolor($new_width, $new_height);
        imagecopyresampled(
            $new_img,                $img,
            0,          0,           0,      0,
            $new_width, $new_height, $width, $height
        );

        switch (true) {
            case ($type === 'image/png; charset=binary'):
                imagepng($new_img, $filename);
                break;
            case ($type === 'image/jpeg; charset=binary'):
                imagejpeg($new_img, $filename);
                break;
            default:
                imagegif($new_img, $filename);
        }

        $new_filename = './tmp/'.basename($filename);
        if (move_uploaded_file($filename,$new_filename))
            $html .= sprintf('<p><img src="%s" /></p>'.PHP_EOL, $new_filename);

    }

}

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Resizer</title>
<style>
label { display: block; }
</style>
</head>
<body>
<fieldset>
<legend>Select Image File (PNG, JPEG, GIF available)</legend>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<label><input type="file" name="images[]" /></label>
<label><input type="file" name="images[]" /></label>
<label><input type="file" name="images[]" /></label>
<label><input type="submit" value="Resize!" /></label>
</form>
</fieldset>
<fieldset>
<legend>Resized Images</legend><?php 

echo $html; 

?>
</fieldset>
</body>
</html>
于 2013-05-11T16:19:01.713 回答