Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress前端帖子表单-图像未上载_Php_Wordpress - Fatal编程技术网

Php Wordpress前端帖子表单-图像未上载

Php Wordpress前端帖子表单-图像未上载,php,wordpress,Php,Wordpress,我正在编写一个插件,允许用户通过填写表单和上传图像从前端发布。问题是图像没有上传,但我可以在我的库中看到它的名称 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*</la

我正在编写一个插件,允许用户通过填写表单和上传图像从前端发布。问题是图像没有上传,但我可以在我的库中看到它的名称

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>
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');
            }
        },
    });
});
});
jQuery(文档).ready(函数($){
$(#fep new post')。on('submit',函数(e){
e、 预防默认值();
var数据=新的FormData(本);
var files=$('#file').val();
console.log(文件);
data.append('file',files);
data.append('action','fep_add_post');
$.ajax({
url:post_livre_submit,
键入:“post”,
数据:数据,
contentType:false,
processData:false,
成功:功能(数据){
如果(新帖子!=0){
$('form#fep new post#result').html(“+data.message+”);
}否则{
警报(“文件未上载”);
}
},
});
});
});
另外,我检查了我的上传文件夹,图像不在那里,我的库中的路径名是C:\fakepath\image2.png

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>";
        }
    }
if(设置($\u POST['html-upload'])&&&!空($\u文件)){
require_once(ABSPATH.'wp admin/includes/admin.php');
$id=media\u handle\u upload('async upload',$post\u id);//客户端文件页面的post id
取消设置($\u文件);
如果(是错误($id)){
$errors['upload_error']=$id;
$id=false;
}
如果($errors){
echo“上传文件时出错。

”; }否则{ echo“您的文件已上载。

”; } }

不应该将
$\u POST['file']
改为
$\u FILES['file']
吗?这样做不会得到文件名My bad。它应该是
$\u FILES['image']
,因为输入字段的名称是
image
。我最终通过使用
$attachment\u id=media\u handle\u upload('image',$\u POST['file'])解决了这个问题进行上传
 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>";
        }
    }