2014-02-28 2 views
1

나는 Wordpress에서 간단한 프런트 엔드 업로드 작업을하고 있습니다. 이 코드 조각을 사용하여 WordPress 미디어 섹션에 업로드 할 수 있지만 파일의 이름과 URL 만 업로드하면됩니다. 이미지가 없습니다.Wordpress file upload no image

이미지 업로드를 위해 내가 무엇을해야하는지 알 수 있습니까?

내 양식 :

<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); 

} 

답변

1

업로드가 완료되면, 이미지 파일이 임시 폴더에 저장되어, 당신이 그것을 복사하지 않을 경우 삭제됩니다 어딘가에. 당신은 당신이 당신이 그것을 필요로

move_uploaded_file(); 

로 이동할 수 있습니다 경로를 파일 줄 것이다

$_FILES['file']['tmp_name']; 

사용하여 파일을 얻을 수 있습니다.

+0

좋아요 감사합니다! 그러나이 Wordpress 예제에서 어디에서이 작업을 수행해야합니까? – Robbert

+0

나는 워드 프레스 코드로 당신을 도울 수 없다. 나는 일반적으로 PHP의 예를 들었다. 따라서 WordPress가 예상하는 파일을 저장하는 방법을 알아 내고 필요할 경우 추가로 처리해야합니다. –

+0

감사합니다. 시도해 보겠습니다. – Robbert

1

WordPress codex에는 "파일이 업로드 디렉토리에 있어야합니다"라고 나와 있습니다. 업로드 된 파일을 업로드 디렉토리로 이동하고 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); 

} 
0
$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);