Wordpress文件上传没有图片

时间:2014-02-28 10:04:12

标签: php wordpress file-upload upload

我正在使用Wordpress进行简单的前端图片上传。使用这段代码,我可以上传到wordpress媒体部分,但我只上传文件的名称和网址,然后没有图片。

有人知道我上传图片需要做什么吗?

我的表格:

<form id="dash_action" method="post" enctype="multipart/form-data">

   <input type="file" name="file" /> 

   <input name="submit" type="submit" class="button" value="<?php _e( 'Save settings' ); ?>" />

</form><!--End dash_action-->

我的PHP表单处理:

if( isset( $_POST['submit'] ) ) {

    $filename = $_FILES['file']['name'];
    $wp_filetype = wp_check_filetype( basename($filename), null );
    $wp_upload_dir = wp_upload_dir();
    $attachment = array(
        'guid' => $wp_upload_dir['subdir'] . '/' . basename( $filename ), 
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
        'post_content' => '',
        'post_status' => 'inherit'
    );
    $attach_id = wp_insert_attachment( $attachment, $filename, 37 );
    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 );  

}

3 个答案:

答案 0 :(得分:1)

上传完成后,图像文件存储在临时文件夹中,如果不将其复制到某处,将被删除。您可以使用

获取文件
$_FILES['file']['tmp_name'];

这将为您提供文件路径,您可以使用

移动
move_uploaded_file();

你需要的地方。

答案 1 :(得分:1)

WordPress codex表示“文件必须在上传目录中”。我添加了一行来将上传的文件移动到uploads目录并更改了guid路径。

if( isset( $_POST['submit'] ) ) {

    $filename = $_FILES['file']['name'];
    $wp_filetype = wp_check_filetype( basename($filename), null );
    $wp_upload_dir = wp_upload_dir();

    // Move the uploaded file into the WordPress uploads directory
    move_uploaded_file( $_FILES['file']['tmp_name'], $wp_upload_dir['path']  . '/' . $filename );

    $attachment = array(
        'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), 
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
        'post_content' => '',
        'post_status' => 'inherit'
    );

    $filename = $wp_upload_dir['path']  . '/' . $filename;

    $attach_id = wp_insert_attachment( $attachment, $filename, 37 );
    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 );  

}

答案 2 :(得分:1)

$upload = wp_upload_bits( $_FILES['file']['name'], null, file_get_contents( $_FILES['file']['tmp_name'] ) );

$wp_filetype = wp_check_filetype( basename( $upload['file'] ), null );

$wp_upload_dir = wp_upload_dir();

$attachment = array(
    'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $upload['file'] ),
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename( $upload['file'] )),
    'post_content'   => '',
    'post_status'    => 'inherit'
    );

$attach_id = wp_insert_attachment( $attachment, $upload['file'],  $post_id);

require_once(ABSPATH . 'wp-admin/includes/image.php');

$attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
wp_update_attachment_metadata( $attach_id, $attach_data );

set_post_thumbnail(  $post_id, $attach_id );
update_post_meta( $post_id, '_thumbnail_id', $attach_id );