如果希望在不使用函数文件的情况下实现此功能,可以使用我提出的简化版本。这是测试代码100%工作
<form id="file_upload" method="post" action="#" enctype="multipart/form-data">
<input type="file" name="my_file_upload[]" multiple="multiple">
<input name="my_file_upload" type="submit" value="Upload" />
</form>
将PHP代码放在发生提交的页面上。在我的情况下,表单操作设置为“#”
<?php if ($_SERVER[\'REQUEST_METHOD\'] == \'POST\') {
require_once( ABSPATH . \'wp-admin/includes/image.php\' );
require_once( ABSPATH . \'wp-admin/includes/file.php\' );
require_once( ABSPATH . \'wp-admin/includes/media.php\' );
$files = $_FILES["my_file_upload"];
foreach ($files[\'name\'] as $key => $value) {
if ($files[\'name\'][$key]) {
$file = array(
\'name\' => $files[\'name\'][$key],
\'type\' => $files[\'type\'][$key],
\'tmp_name\' => $files[\'tmp_name\'][$key],
\'error\' => $files[\'error\'][$key],
\'size\' => $files[\'size\'][$key]
);
$_FILES = array("upload_file" => $file);
$attachment_id = media_handle_upload("upload_file", 0);
if (is_wp_error($attachment_id)) {
// There was an error uploading the image.
echo "Error adding file";
} else {
// The image was uploaded successfully!
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
}
}
}
} ?>
当表单提交完成时,此方法只包含一次所需的文件,而不是每次通过foreach循环调用函数时都包含它们