我要疯了..我正在尝试上传两个文件,但没有结果
<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作为结果
答案 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++;
}
}