使用多个输入进行多个图像上传

时间:2015-06-20 14:35:01

标签: php

我尝试从多个输入元素上传多个文件以用于在线申请表单。我可以使用下面的脚本上传一个图像请分享我如何使用多个输入上传多个图像?

if(!empty($_FILES)){
    include 'config.php';
    $file = $_FILES['image_file'];
    $file_name = $file['name'];
    $error = ''; // Empty
    $ext = strtolower(substr(strrchr($file_name, "."), 1));

    if($validation_type == 1)   
    {
        $file_info = getimagesize($_FILES['image_file']['tmp_name']);
        if(empty($file_info)) // No Image?      
        {
            $error .= "The uploaded file doesn't seem to be an image.";              
        }
        else // An Image?      
        {
            $file_mime = $file_info['mime'];
            if ($ext == 'jpc' || $ext == 'jpx' || $ext == 'jb2')         
            {
                $extension = $ext;         
            }
            else
            {
                $extension = ($mime[$file_mime] == 'jpeg') ? 'jpg' : $mime[$file_mime];
            }    
            if(!$extension)
            {
                $extension = '';  
                $file_name = str_replace('.', '', $file_name); 
            }     
        }   
    }
    else if($validation_type == 2)
    {     
        if (!in_array($ext, $image_extensions_allowed))
        {
            $exts = implode(', ',$image_extensions_allowed);
            $error .= "You must upload a file with one of the following extensions:".$exts;
        }
        $extension = $ext;   
    }

    if($error == "") // No errors were found?
    {
        $new_file_name = strtolower($file_name);
        $new_file_name = str_replace(' ', '-', $new_file_name);
        $new_file_name = substr($new_file_name, 0, -strlen($ext));
        $new_file_name .= $extension;   // File Name
        $move_file = move_uploaded_file($file['tmp_name'],     
        $upload_image_to_folder.$new_file_name);
        if($move_file)
        {
           $done = 'The image has been uploaded.';
        }
    }
    else
    {
        @unlink($file['tmp_name']);
    }
    $file_uploaded = true;
}

1 个答案:

答案 0 :(得分:1)

您可能正在寻找多个浏览按钮,请尝试使用以下字段名称:

<input type="file" name="image_file1[]" />
<input type="file" name="image_file1[]" />
<input type="file" name="image_file1[]" />
<input type="file" name="image_file2[]" />
<input type="file" name="image_file2[]" />
<input type="file" name="image_file2[]" />

然后

                if(!empty($_FILES['image_file1']['name'])){                 
                $uploaded_file = $_FILES['image_file1'];
                for($fi=0; $fi<count($_FILES['image_file1']['name']); $fi++){
                   $file_name= $uploaded_file['name'][$fi];
                   $file_type= $uploaded_file['type'][$fi];
                   $tmp_name= $uploaded_file['tmp_name'][$fi];
                   $file_size= $uploaded_file['size'][$fi];
                }
                }



                if(!empty($_FILES['image_file2']['name'])){                 
                $uploaded_file = $_FILES['image_file2'];
                for($fi=0; $fi<count($_FILES['image_file2']['name']); $fi++){
                   $file_name= $uploaded_file['name'][$fi];
                   $file_type= $uploaded_file['type'][$fi];
                   $tmp_name= $uploaded_file['tmp_name'][$fi];
                   $file_size= $uploaded_file['size'][$fi];
                }
                }