Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

php - Multiple Upload Forms

I have a number of upload file forms that need validating (shortened example):

<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>

And I want each upload input to be validated by file type, but I'm having a hard to understanding what needs to be done. This is the kind of thing I'm trying, but evidently it's not right!

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

But I can't figure out how to select, for example, the first upload field - I've tried using $_FILES['file']['name'][0] but to no avail. Any hep would be appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By chance, I wrote the following script yesterday.
This is for resizing images, PNG or GIF or JPEG.
This requires './tmp' directory.
If you like, please refer to this.

<?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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...