我在wordpress中创建了一个页面,并且我已经放了这个表单。
<form action=""method="post"enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="text" placeholder="Your Name" name="name" />
<input type="submit" name="submit" value="Submit">
</form>
现在点击提交我希望用户上传的图片保存在文件夹中wp-content / uploads / [发布的用户名]
任何人都可以帮我解决wordpress中的后端处理部分。我已经尝试过wp_upload_bits,但是不能用它。
提前致谢。
答案 0 :(得分:2)
wordpress的用户insert_attachment以保存附件,然后您还将具有获取具有不同样式的图像的功能,get_attachment(id,'full / medium / thumbnail')。
$files = $_FILES['upload_attachment'];
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,null);
// save the $newupload with you from data, as psot id for the attachment
}
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;
}
在html中,如果您提交表单,图片将被上传,并且referances将保存在wp_post表中
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="upload_attachment[]" id="file"><br>
<label for="file">Filename:</label>
<input type="file" name="upload_attachment[]" id="file"><br>
<label for="file">Filename:</label>
<input type="file" name="upload_attachment[]" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>