2014-12-02 89 views
0

在前端,我創建了一個表格,使用upload_user_file()從前端主題上傳文件,並且每個圖片在WordPress媒體庫中都存儲正常(好像是這樣)。WordPress前端上傳問題

所以,當我上傳一個名爲test.jpg的文件時,它的相關縮略圖就會在媒體庫中創建並顯示出來。 測試150x150.jpg 測試300x225.jpg 測試1024x768.jpg

我是當我從媒體庫中刪除這一形象的問題。只有創建的縮略圖被刪除,test.jpg保留在上傳文件夾中。如果我直接從媒體庫上傳此文件,然後刪除它,則刪除所有文件,包括test.jpg

下面是在數據庫中存儲值並將文件上傳到中間庫的代碼。是否有另一個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']); 

與此:

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

在wp_postmeta,對於上傳meta_value設置爲完整的URL(http://sitenamen.com/wp-content/upload/2014/12/file.jpg),而應貯存這樣的: 2014/12/file.jpg

現在所有文件都被刪除。