php 表单自定义表单文件上传附件多附件表单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 表单自定义表单文件上传附件多附件表单相关的知识,希望对你有一定的参考价值。

<?php /* This function attaches the image to the post in the database, add it to functions.php */
function insert_attachment($file_handler,$post_id,$setthumb='false') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
} ?> 

<?php /* Also in the template file, this script processes any $_POST data and hands off the images to the insert_attachment() function. Again you may want to wrap this in a current_user_can conditional, and probably do some sanity checks on incoming $_POST data. */
global $post;
if ( $_FILES ) {
$files = $_FILES['upload_attachment'];
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_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post->ID);
}
}
}
} ?> 

<!-- This is the form that you add to the template file. You probably want to wrap this in a current_user_can conditional. -->
 
<form action="#" method="post" enctype="multipart/form-data"><input type="file" name="upload_attachment[]" multiple /> <input type="submit" value="Upload" /></form>

以上是关于php 表单自定义表单文件上传附件多附件表单的主要内容,如果未能解决你的问题,请参考以下文章

php 带附件的自定义表单

简单的 PHP 表单:电子邮件附件(代码高尔夫)

来自表单的 WP 邮件附件,没有在文件管理器上上传文件

js 提交表单数据: 附件+对象的的多集合

电子邮件表单根据附件发送多封邮件

表格提交文件附件