2016-08-30 36 views
1
require_once(ABSPATH . '/wp-load.php'); 
          require_once(ABSPATH . '/wp-admin/includes/file.php'); 
          require_once(ABSPATH . '/wp-admin/includes/image.php'); 

          $upload_overrides = array('test_form' => FALSE); 
          $count_files = count($_FILES['my_files']); 
          $uploads = wp_upload_dir(); 
          foreach (range(0, $count_files) as $i) { 

           // create an array of the $_FILES for each file 
           $file_array = array(
            'name'  => $_FILES['files']['name'][$i], 
            'type'  => $_FILES['files']['type'][$i], 
            'tmp_name' => $_FILES['files']['tmp_name'][$i], 
            'error' => $_FILES['files']['error'][$i], 
            'size'  => $_FILES['files']['size'][$i], 
           ); 

           // check to see if the file name is not empty 
           if (!empty($file_array['name'])) { 

            // upload the file to the server 
            $uploaded_file = wp_handle_upload($file_array, $upload_overrides); 

            // checks the file type and stores in in a variable 
            $wp_filetype = wp_check_filetype(basename($uploaded_file['file']), null); 
            if ($uploaded_file && !isset($uploaded_file['error'])) { 
               $ufiles = get_post_meta($post_id, 'my_files', true); 
               if(empty($ufiles)) $ufiles = array(); 
               $ufiles[] = $uploaded_file; 
               update_post_meta($post_id, 'my_files', $ufiles); 

            } 
           } 
          } 

由於此代碼,我可以將文件下載到metabox。Wordpress刪除帶有文件的metabox上的下載圖像

數據庫的輸出是看起來像什麼,我表現出以下

a:2:{i:0;a:3:{s:4:"file";s:48:"D:xampphtdocswp/wp-content/uploads/2016/08/2.jpg";s:3:"url";s:52:"http://localhost/wp/wp-content/uploads/2016/08/2.jpg";s:4:"type";s:10:"image/jpeg";}i:1;a:3:{s:4:"file";s:59:"D:xampphtdocswp/wp-content/uploads/2016/08/2da83a4s-960.jpg";s:3:"url";s:63:"http://localhost/wp/wp-content/uploads/2016/08/2da83a4s-960.jpg";s:4:"type";s:10:"image/jpeg";}} 

我要刪除我不想與delete_post_meta方法,而我是我的更新頁面上選擇複選框的圖像。

      $galleri = get_post_meta($id,'my_files',true); 



<div class="galeri"> 

    <?php 
    foreach($galleri as $galeri){ 

echo "<div style='margin:10px;display:inline-block;'><input type='checkbox' name='car_image_delete[]' value='".$galeri['url']."' /><img src='".$galeri['url']."' width='150' height='150'/></div>"; 
             } 


            ?> 
           </div> 

我很感激,如果你幫我

回答

0

試試這個:

使用update_post_meta()函數代替delete_post_meta()

使用delete_post_meta()時,它會刪除自定義字段。

所以,如果你想刪除特定的一個文件。您需要使用update_post_meta()

$string = 'a:2:{i:0;a:3:{s:4:"file";s:48:"D:xampphtdocswp/wp-content/uploads/2016/08/2.jpg";s:3:"url";s:52:"http://localhost/wp/wp-content/uploads/2016/08/2.jpg";s:4:"type";s:10:"image/jpeg";}i:1;a:3:{s:4:"file";s:59:"D:xampphtdocswp/wp-content/uploads/2016/08/2da83a4s-960.jpg";s:3:"url";s:63:"http://localhost/wp/wp-content/uploads/2016/08/2da83a4s-960.jpg";s:4:"type";s:10:"image/jpeg";}}'; 
$arr = unserialize($string); //USE get_post_meta() function instead of 
$index = array_search('http://localhost/wp/wp-content/uploads/2016/08/2da83a4s-960.jpg',array_column($arr, 'url')); //search index 
echo $index; 
if (array_key_exists($index,$arr)) 
{ 
    unset($arr[$index]); //remove array index 
} 
print_r($arr); //array with only value. 
//use array_values() to reindex 
update_post_meta($post_id, 'my_files', $arr); //update post meta 
+0

警告:反序列化()預計參數1是串,陣列中的給定的d:\ XAMPP \ htdocs中\ WP \可溼性粉劑內容\主題\ snarent \ page_edit.php上線147 警告:array_column( )期望參數1是數組,布爾給出D:\ xampp \ htdocs \ wp \ wp-content \ themes \ snarent \ page_edit.php on line 148 警告:array_search()期望參數2是數組,null給定在D:\ xampp \ htdocs \ wp \ wp-content \ themes \ snarent \ page_edit.php on line 148 警告:array_key_exists()期望參數2是數組,布爾值在D:\ xampp \ htdocs \ wp \ wp-content \ themes \ snarent \ page_edit.php 150行 –

+0

你有沒有添加$字符串值或另一個值? @ M.UNLU – vrajesh

+0

$ metatable = $ wpdb->前綴。「postmeta」; \t \t \t \t \t \t \t \t \t $字符串= $ wpdb-> get_var($ wpdb->製備( 「選擇meta_value FROM $元表,其中POST_ID =%d AND meta_key = 'my_files'」,$ ID)); \t \t \t \t \t \t \t \t \t $ ARR =反序列化($字符串); // USE get_post_meta()函數而不是 \t \t \t \t \t \t \t \t \t $指數= array_search($ car_image_delete [0],array_column($ ARR, 'URL')); //搜索索引 \t \t \t \t \t \t \t \t \t回聲$指數; \t \t \t \t \t \t \t \t \t如果(array_key_exists($索引,$ ARR)) \t \t \t \t \t \t \t \t \t { \t \t \t \t \t \t \t \t \t \t未設置($ ARR [$索引]); //刪除數組索引 \t \t \t \t \t \t \t \t \t} –