2014-12-02 3 views
0

프론트 엔드에서 나는 앞뒤가 뒤덮인 테마에서 파일을 업로드하기 위해 upload_user_file()을 사용하는 양식을 만들었으며 모든 이미지는 WordPress 미디어 라이브러리 (물론 그렇게 보입니다)에 잘 저장되어 있습니다.WordPress 프론트 엔드 업로드 문제

그래서 test.jpg이라는 파일을 업로드하면 상대적인 미리보기 이미지가 만들어지고 미디어 라이브러리에 표시됩니다. 시험 150x150.jpg 시험 300x225.jpg 시험 1024x768.jpg

나는 미디어 라이브러리에서이 이미지를 삭제할 때이다이 문제. 만들어진 미리보기 이미지 만 삭제되고 test.jpg은 그대로 업로드 폴더에 남습니다. 미디어 라이브러리에서이 파일을 직접 업로드 한 다음 삭제하면 test.jpg을 포함하여 모든 파일이 삭제됩니다.

데이터베이스에 값을 저장하고 파일을 medialibrary에 업로드하는 코드입니다. 사용할 다른 WordPress 기능이 있습니까? upload_user_file()가 데이터베이스에 이미지 데이터를 올바르게 저장하지 않는다고 생각하십니까?

global $wpdb; 
global $post; 

//$table = 'wp_verk1_project'; //$post_slug=$post->post_name; 
$table = $wpdb->prefix . "project_name_" . $post_slug=$post->post_name; 
$data = array(
    'contributorname' => $_POST['yourname'], 
    'email' => $_POST['email'], 
    'telephone' => $_POST['telephone'], 
    'description' => $_POST['description'], 
    'date' => date('Y-m-d'), 
    'time' => date('H:i:s'), 
    'upload' => upload_user_file($_FILES['file']), 
    'upload2' => upload_user_file($_FILES['file2']), 
    'upload3' => upload_user_file($_FILES['file3']), 
    'upload4' => upload_user_file($_FILES['file4']), 
    'upload5' => upload_user_file($_FILES['file5']), 
    'rate' => '0' 
); 
$format = array(
    '%s', 
    '%s' 
); 

$success = $wpdb->insert($table, $data, $format); 

if ($success) { 
    header("Location: " . get_bloginfo('url') . "/thank-you/"); 
    exit(); 
} 

edit_user_file은() :

function upload_user_file($file = array()) { 

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

    $file_return = wp_handle_upload($file, array('test_form' => false)); 

    if(isset($file_return['error']) || isset($file_return['upload_error_handler'])) { 
     return false; 
    } else { 

     $filename = $file_return['file']; 

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

     $attachment_id = wp_insert_attachment($attachment, $file_return['url']); 

     require_once (ABSPATH . 'wp-admin/includes/image.php'); 
     $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); 
     wp_update_attachment_metadata($attachment_id, $attachment_data); 

     if(0 < intval($attachment_id)) { 
      return $attachment_id; 
     } 
    } 

    return false; 
} 

종류는 요한

답변

1

내가 내 자신의 질문을 해결하기 위해 관리 간주한다.

WordPress 코어 파일을 파고 들었습니다.

Ireplaced 다음 줄이와

$attachment_id = wp_insert_attachment($attachment, $file_return['url']); 

: wp_postmeta에서

$attachment_id = wp_insert_attachment($attachment, $filename); 

는 업로드 meta_value은이 같은 저장되어야하는 동안 전체 URL (http://sitenamen.com/wp-content/upload/2014/12/file.jpg)로 설정 : 2014/12/file.jpg

이제 모든 파일이 삭제됩니다.

관련 문제