2013-04-09 83 views
1

我使用下面的代碼將文件移動到名爲「training」的文件夾中。將文件上傳到wordpress中的文件夾

<td> 
    <input type="file" name="training_image" accept="image/*" id="traing-image"value="<?php echo esc_attr(get_the_author_meta('training_image', $user->ID)); ?>" class="regular-text" /> 
    </td> 

要保存圖像我寫這篇

$training_path = time().'_'.$_FILES['training_image']['name']; 
$tmp_file_name = $_FILES['training_image']['tmp_name']; 
$upload_dir = wp_upload_dir(); 
if($training_path){ 
     move_uploaded_file($tmp_file_name,$upload_dir['baseurl'].'/training/'.$training_path); 
} 
update_user_meta($user_id, 'training-image', $training_path); 

,但我無法得到任何結果。我做錯了嗎?請幫幫我。

這是我的全部代碼

function training_user_profile_fields($user) { ?> 
<h3><?php// _e("Extra profile information", "blank"); ?></h3> 

<table class="form-table"> 
    <tr> 
    <th><label for="traing-image"><?php _e("Upload Your Training Certificate:"); ?></label></th> 
     <td> 
     <input type="file" name="training_image" id="traing-image"value="<?php echo esc_attr(get_the_author_meta('training_image', $user->ID)); ?>" class="regular-text" /> 
     </td> 
    </tr> 
</table> 
<?php } 

add_action('personal_options_update', 'save_extra_user_profile_fields'); 
add_action('edit_user_profile_update', 'save_extra_user_profile_fields'); 

function save_extra_user_profile_fields($user_id) { 

if (!current_user_can('edit_user', $user_id)) { return false; } 
$training_path = time().'_'.$_FILES['training_image']['name']; 
$tmp_file_name = $_FILES['training_image']['tmp_name']; 
echo "filename".$_FILES['training_image']['tmp_name']; exit; 
$upload_dir = wp_upload_dir(); 
if($training_path){ 
     move_uploaded_file($tmp_file_name,$upload_dir['baseurl'].'/training/'.$training_path); 
} 
update_user_meta($user_id, 'training-image', $training_path); 
} 
+0

你能告訴你的完整形式 – Sabari 2013-04-09 08:46:29

回答

0

你做錯了。 $upload_dir['baseurl']URL上傳目錄,而不是absolute path到目錄。您無法使用URL上傳或移動文件。

HTTP wrapper does not support writeable connections

所以$upload_dir['baseurl']應更改爲upload_dir $ [ 'BASEDIR']。

應該

$training_path = time().'_'.$_FILES['training_image']['name']; 
$tmp_file_name = $_FILES['training_image']['tmp_name']; 
$upload_dir = wp_upload_dir(); 
if($training_path){ 
     move_uploaded_file($tmp_file_name,$upload_dir['basedir'].'/training/'.$training_path); 
} 
update_user_meta($user_id, 'training-image', $training_path); 

另外,還要確保你的表單的enctype設置爲multipart/form-data(enctype="multipart/form-data")

UPDATE

WordPress的配置文件形式不具有默認enctype屬性。通過你可以添加一個鉤子。在你的主題添加此functions.php

add_action('user_edit_form_tag', 'add_enctype'); 

function add_form_enctype() { 
    echo 'enctype="multipart/form-data"'; 
} 

希望這有助於

+0

沒有當我打印$ training_path。我只能找到時間和'_',但我找不到$ _FILES ['training_image'] ['name'] ;.所以我很困惑。 – Ranjit 2013-04-09 08:54:36

+0

@Ranjit可能你沒有在你的表單中設置enctype。請粘貼您的完整表格。在表單標籤中設置enctype =「multipart/form-data」 – Sabari 2013-04-09 09:02:22

+0

這是wordpress用戶配置文件表單。並在表單中添加額外的字段。 – Ranjit 2013-04-09 09:07:47

相關問題