我正在编写一个插件,允许用户通过填写表单并上传图片从前端发帖。问题是图像没有上传,但我可以在我的库中看到它的名字。
HTML表格
<form id="fep-new-post" name="new_post" method="post" enctype="multipart/form-data">
<div id="result">
</div>
<p><label>Nom de l'ouvrage*</label><input type="text" id ="nom" name="nom" /></p>
<p><label>Auteur*</label><input type="text" id ="auteur" name="auteur" /></p>
<p><label>Nombre de pages *</label><input type="text" id ="nombre_de_pages" name="nombre_de_pages" /></p>
<p><label>Catégorie *</label><input type="text" id ="categorie" name="categorie" /></p>
<p><label>Résumé*</label><textarea class="fep-content" name="resume" id="resume" tabindex="1" rows="4" cols="60"></textarea></p>
<input type='file' id="file" name='image' data-allowed-file-extensions='["png", "jpg", "gif", "jpeg"]' accept='image/*'>
<input id="submit" type="submit" tabindex="3" />
</form>
处理PHP
function fep_add_post(){
$meta=[];
$meta['nom']= $_Post['nom'];
$meta['auteur']= $_Post['auteur'];
$meta['nombre_de_pages']= $_Post['nombre_de_pages'];
$meta['categorie']= $_Post['categorie'];
$meta['resume']= $_Post['resume'];
$post_id = array(
'post_title' => wp_strip_all_tags($nom),
'post_type' => 'livre',
'post_author' => get_current_user_id(),
'meta_input' => $meta,
'post_status' => 'pending'
);
$new_post = wp_insert_post($post_id);
$filename = $_POST['file'];
$parent_post_id = $new_post;
$filetype = wp_check_filetype( basename( $filename ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $parent_post_id, $attach_id );
if ($new_post == 0 ){
echo json_encode( array('loggedin' => false, 'message' => __('Echec dinsertion')));
} else{
echo json_encode( array('loggedin' => true, 'message' => __('Insertion effectuer')));
}
die();
}
JS
jQuery( document ).ready( function ( $ ) {
$( '#fep-new-post' ).on( 'submit', function(e){
e.preventDefault();
var data = new FormData(this);
var files = $('#file').val();
console.log(files);
data.append('file',files);
data.append('action', 'fep_add_post');
$.ajax({
url: post_livre_submit,
type: 'post',
data: data,
contentType: false,
processData: false,
success: function(data){
if(new_post!= 0){
$( 'form#fep-new-post #result' ).html("<h2>"+data.message+"</ph2>");
}else{
alert('file not uploaded');
}
},
});
});
});
此外,我确实检查了我的uploads文件夹,但图片不存在,我的库中的路径名是C:\ fakepath \ image2.png
答案 0 :(得分:0)
media_handle_upload()
保存从POST请求提交的文件并创建附件 为此发布。
我将添加以下示例,您必须根据自己的需要进行修改:
if ( isset( $_POST['html-upload'] ) && ! empty( $_FILES ) ) {
require_once( ABSPATH . 'wp-admin/includes/admin.php' );
$id = media_handle_upload( 'async-upload', $post_id ); //post id of Client Files page
unset( $_FILES );
if( is_wp_error( $id ) ) {
$errors['upload_error'] = $id;
$id = false;
}
if( $errors ) {
echo "<p>There was an error uploading your file.</p>";
} else {
echo "<p>Your file has been uploaded.</p>";
}
}
https://developer.wordpress.org/reference/functions/media_handle_upload/