Php 从前端表单设置特征图像
所以我在互联网上搜寻了几天,寻找解决这个问题的方法。我正试图通过此表单的上载输入设置帖子的特色图像:Php 从前端表单设置特征图像,php,wordpress,validation,file-upload,Php,Wordpress,Validation,File Upload,所以我在互联网上搜寻了几天,寻找解决这个问题的方法。我正试图通过此表单的上载输入设置帖子的特色图像: <form method="post" action="" id="quick-post-form" enctype="multipart/form-data"> <textarea id="input-box" placeholder="What's on your mind?" maxlength="10000" name="quick-post-area" cla
<form method="post" action="" id="quick-post-form" enctype="multipart/form-data">
<textarea id="input-box" placeholder="What's on your mind?" maxlength="10000" name="quick-post-area" class="col-xs-12"></textarea>
<!-- images -->
<fieldset class="images">
<label for="images">Upload an image</label>
<input type="file" name="image-uploader" id="img-upload" size="50">
</fieldset>
<!-- images end -->
<input type="submit" value="Post" name="submit" class="col-md-4 col-xs-12" id="quick-post-submit">
</form>
如果有人能告诉我哪里出了问题,我将不胜感激
谢谢见谢谢,诺曼。我现在有一个运行良好的函数,但我正在尝试使用以下方法检查文件类型:是否可以在提交表单之前回显mime类型?只有在使用get方法或从其他源读取文件时,才能在提交表单之前检查文件类型。如果您依赖于用户上载图像,那么您的代码将验证文件类型,您需要在between-post方法中包装您的功能。您的意思是在get方法中包装它吗?这就是我正在使用的,理想情况下,我希望它在点击提交之前检查文件类型:dude您需要使用jquery验证引擎进行表单验证。它将在客户端验证图像文件类型,还可以远程验证用户选择的图像。
<?php
function quick_post() {
// Initialize the page ID to -1. This indicates no action has been taken.
$post_id = -1;
$author_id = 1;
$slug = 'post';
global $current_user;
$user_id = get_current_user_id();
// If the page doesn't already exist, then create it
if( null == get_page_by_title( $title ) ) {
$post_id = wp_insert_post(
array(
'comment_status' => 'open',
'ping_status' => 'closed',
'post_author' => $user_id,
'post_name' => $slug,
'post_title' => 'Post By: ' . $current_user->user_login,
'post_status' => 'publish',
'post_type' => 'post',
'post_content' => $_POST['quick-post-area'],
'post_category' => array( 3 ),
)
);
require_once (ABSPATH.'/wp-admin/includes/media.php');
require_once (ABSPATH.'/wp-admin/includes/file.php');
require_once (ABSPATH.'/wp-admin/includes/image.php');
$attachmentId = media_handle_upload('image-uploader', $post_ID);
set_post_thumbnail($post_ID, $attachmentId);
// Otherwise, we'll stop
} else {
// Arbitrarily use -2 to indicate that the page with the title already exists
$post_id = -2;
} // end if
} // end programmatically_create_post
if(isset($_POST['submit']))
{
quick_post();
header('Location: https://outrankd.com');
}
?>
if ($attachmentId = wp_check_filetype('image')) {
set_post_thumbnail($post_ID, $attachmentId);
}