我正在为一个名为“CPT”的项目提交前端帖子;empresas公司;使用CMB2,我创建了一个名为portfolio的自定义字段,该字段的类型是file\\u list,以便业务部门可以上传他们的工作
在我的自定义表单中,我有一个用于上载公司徽标的输入文件,还有其他多个用于上载其工作的输入文件
我正在使用以下代码将徽标上传为帖子的特色图像,并且没有任何问题
if($_FILES) {
foreach( $_FILES as $file => $array ) {
if($_FILES[$file][\'error\'] !== UPLOAD_ERR_OK){
return "upload error : " . $_FILES[$file][\'error\'];
}
$attach_id = media_handle_upload( $file, $post_id );
var_dump($attach_id);
}
}
if($attach_id > 0) {
update_post_meta( $post_id,\'_thumbnail_id\', $attach_id );
}
问题是,当我添加输入的多个文件和代码时,它不会上载任何图像,我尝试了this code 做了一点修改,但不起作用上载多个文件的代码为
$otherfiles = $_FILES[\'trabajos_empresa\'];
foreach ( $otherfiles[\'name\'] as $key => $value ) {
if ( $otherfiles[\'name\'][ $key ] ) {
$file = array(
\'name\' => $otherfiles[\'name\'][ $key ],
\'type\' => $otherfiles[\'type\'][ $key ],
\'tmp_name\' => $otherfiles[\'tmp_name\'][ $key ],
\'error\' => $otherfiles[\'error\'][ $key ],
\'size\' => $otherfiles[\'size\'][ $key ],
);
$_FILES[\'trabajos_empresa\'] = $file;
$attachment_id = media_handle_upload( \'trabajos_empresa\', $post_id );
update_post_meta( $post_id,\'_studiare_portafolio\', $attachment_id );
var_dump($attachment_id);
if ( is_wp_error( $attachment_id ) ) {
// There was an error uploading the image.
echo \'Error adding file\';
} else {
// The image was uploaded successfully!
//Echo to see if image upload fine
echo \'File added successfully with ID: \' . $attachment_id . \'<br>\';
echo wp_get_attachment_image( $attachment_id, array( 800, 600 ) ) . \'<br>\'; // Display the uploaded image with a size you wish. In this case it is 800x600
}
}
}
im使用的html是 <input type="file" name="post_image" id="post_image" aria-required="true">
<input type="file" name="trabajos_empresa[]" id="trabajos_empresa" aria-required="true" multiple="multiple">
如果我进行var\\u转储,我会将所有文件上载到输入trabajos\\u empresa中,但我的媒体库中不会保存任何文件