只挑选从$ _FILES数组上传的文件

时间:2014-08-21 13:24:11

标签: php arrays file-upload

我注意到在使用php上传多个文件时,该数组包含表单中所有输入字段的数据。

假设我有一个带有3个输入字段的from,并且一个用户只上传了2个文件,我怎样才能从数组中选择那两个文件,可能使用某些数组函数或其他任何东西,所以我只能循环通过那些文件。

我在array_filer上尝试使用tmp_name,但无法确定如何继续使用。

if($_FILES && isset($_POST['handle'])) {

    $numFiles = count(array_filter($_FILES['file']['tmp_name']));

    if( $numFiles <= 2 ) {
        // files that were uploaded will not have a blank value
        $filesUploaded = array_filter($_FILES['file']['tmp_name']);

            print_r($filesUploaded);// for testing
            // loop through the above array
            foreach($filesUploaded as $key) {// stuck
                //echo each files attributes
                echo $_FILES['file']['tmp_name'][$key].'<br>';
                echo $_FILES['file']['name'][$key].'<br>';
                echo $_FILES['file']['type'][$key].'<br>';
                echo $_FILES['file']['size'][$key].'<br>';
                echo $_FILES['file']['error'][$key].'<br>';
                // proceed with the rest of the processing
            }
    } else {
        echo 'Too may files uploaded';
    }
} else {
    echo 'Error A';
}

2 个答案:

答案 0 :(得分:0)

你期待这样吗?

检查文件是否已上传:

if(is_uploaded_file($_FILES['image']['tmp_name'])) {}

编辑:

$fileUploads=$_FILES['file']['tmp_name'];
$countUpload=0;
foreach($fileUploads as $fileUpload){
 if($countUpload>2) {
     echo $_FILES['file']['tmp_name'];
     //move_upload_file
 }
 $countUpload=$countUpload+1;
}

答案 1 :(得分:0)

检查错误状态:

if ($_FILES['file']['error'][$key] == 0) {
    echo $_FILES['file']['tmp_name'][$key];
}