上传两个文件

时间:2016-06-23 16:09:38

标签: php wordpress file-upload image-uploading

我要疯了..我正在尝试上传两个文件,但没有结果

<input name="post_image" type="file" class="file">
<input name="post_image_2" type="file" class="file">

和php

if ($_FILES) {
        foreach ($_FILES as $file => $array) {
            if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                echo "upload error : " . $_FILES[$file]['error'];
                die();
            }
            $attach_id = media_handle_upload( $file, $post_id );
        }   
    }

但它循环所有文件并将文件分配给$ attach_id。所以我试着制作

foreach ($_FILES['post_image'] as $file => $array) {
    if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
        echo "upload error : " . $_FILES[$file]['error'];
        die();
        }
        $attach_id = media_handle_upload( $file, $post_id );
    }    

$_FILES['post_image_2']相同 但后来我知道了 Undefined index: name, type, tmp_name, error... in..

我预计$ attach_id和$ attach_id_2作为结果

1 个答案:

答案 0 :(得分:0)

你可以这样做:

if ($_FILES) {
        $i = 1;
        foreach ($_FILES as $file => $array) {
            if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                echo "upload error : " . $_FILES[$file]['error'];
                die();
            }
            $var = 'attach_id_'.$i;
            $$var = media_handle_upload( $file, $post_id );
            $i++;
        }   
    }