如何从html表单中的多个文件数组中选择和发布单个文件并通过php操作

时间:2018-08-27 09:29:13

标签: php html

我打算做-单击“提交1”按钮时,第一张照片将调整大小,单击“提交2”按钮时,第二张照片将分别调整大小,并且也将分别显示。 当我单击summit1按钮时,将调整第一张照片的大小,但是当我单击summit2按钮时,第一张照片消失,而第二张照片出现。

<div class="container">
    <form action="" class="form" method="POST">
        <input type="file" name="upload_image[]" id="upload1">
        <input type="submit" name="submit1" class="btn btn-success" value="submit">
        <input type="file" name="upload_image[]" id="upload2">
        <input type="submit" name="submit2" class="btn btn-success" value="submit">
    </form>
</div>
<?php  
// clicking the submit1 button will resize the photo and display it
if(isset($_POST['submit1'])) 
{ 

    $i=0;
    $name = $_FILES["image_upload"]["name"];
    $size = $_FILES["image_upload"]["size"];
    $le=explode(".", $name[$i]);
    $ext = end($le);
    $allowed_ext = array("png", "jpg", "jpeg");
    if(in_array($ext, $allowed_ext))
    {
        if($size < (1024*1024))
        {
            $new_image = '';
            $new_name = md5(rand()) . '.' . $ext;
            $path = 'upload/' . $new_name;
            list($width, $height) = getimagesize($_FILES["image_upload1"]["tmp_name"][$i]);
            if($ext == 'png')
            {
                $new_image = imagecreatefrompng($_FILES["image_upload1"]["tmp_name"][$i]);
            }
            if($ext == 'jpg' || $ext == 'jpeg')  
            {  
                $new_image = imagecreatefromjpeg($_FILES["image_upload1"]["tmp_name"][$i]);  
            }
            $new_width=200;
            $new_height = ($height/$width)*200;
            $tmp_image = imagecreatetruecolor($new_width, $new_height);
            imagecopyresampled($tmp_image, $new_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            imagejpeg($tmp_image, $path, 100);
            imagedestroy($new_image);
            imagedestroy($tmp_image);
            echo '<img src="'.$path.'" />';
        }
        else
        {
            echo 'Image File size must be less than 1 MB';
        }
    }
}
else
{
    echo 'Invalid Image File';
}

// clicking the submit2 button will resize the photo and display it
if(isset($_POST['submit2'])) 
{ 

    $i=1;
    $name = $_FILES["image_upload2"]["name"];
    $size = $_FILES["image_upload2"]["size"];
    $le=explode(".", $name[$i]);
    $ext = end($le);
    $allowed_ext = array("png", "jpg", "jpeg");
    if(in_array($ext, $allowed_ext))
    {
        if($size < (1024*1024))
        {
            $new_image = '';
            $new_name = md5(rand()) . '.' . $ext;
            $path = 'upload/' . $new_name;
            list($width, $height) = getimagesize($_FILES["image_upload2"]["tmp_name"][$i]);
            if($ext == 'png')
            {
                $new_image = imagecreatefrompng($_FILES["image_upload2"]["tmp_name"][$i]);
            }
            if($ext == 'jpg' || $ext == 'jpeg')  
            {  
                $new_image = imagecreatefromjpeg($_FILES["image_upload2"]["tmp_name"][$i]);  
            }
            $new_width=200;
            $new_height = ($height/$width)*200;
            $tmp_image = imagecreatetruecolor($new_width, $new_height);
            imagecopyresampled($tmp_image, $new_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            imagejpeg($tmp_image, $path, 100);
            imagedestroy($new_image);
            imagedestroy($tmp_image);
            echo '<img src="'.$path.'" />';
        }
        else
        {
            echo 'Image File size must be less than 1 MB';
        }
    }
}
else
{
    echo 'Invalid Image File';
}



?>

0 个答案:

没有答案