2016-08-30 2 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); 

            } 
           } 
          } 

Я могу загрузить файлы в metabolox благодаря этому коду.Wordpress Удаление загруженных изображений на метабокс с файлом

Вывод базы данных выглядит как то, что я показываю в следующей

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 \ в.ч. \ в.ч.-контента \ темы \ snarent \ page_edit.php на линии 147 Внимание: array_column () ожидает, что параметр 1 будет массивом, boolean задан в D: \ xampp \ htdocs \ wp \ wp-content \ themes \ snarent \ page_edit.php в строке 148 Предупреждение: array_search() ожидает, что параметр 2 будет массивом, null в D: \ xampp \ htdocs \ wp \ wp-content \ themes \ snarent \ page_edit.php в строке 148 Предупреждение: array_key_exists() ожидает, что параметр 2 будет массивом, логическим значением в D: \ xampp \ htdocs \ wp \ wp-content \ themes \ snarent \ page_edit.php в строке 150 –

+0

У вас есть добавлено значение $ string или другое значение? @ M.UNLU – vrajesh

+0

$ metatable = $ wpdb-> префикс."PostMeta"; \t \t \t \t \t \t \t \t \t $ строка = $ wpdb-> get_var ($ wpdb-> подготовить ("SELECT meta_value ОТ $ метатаблица, где post_id =% d и meta_key = 'my_files'", $ Id)); \t \t \t \t \t \t \t \t \t $ обр = десериализации ($ строка); // ИСПОЛЬЗОВАНИЕ get_post_meta() функция вместо \t \t \t \t \t \t \t \t \t $ указательным = array_search ($ car_image_delete [0], array_column ($ обр, 'URL')); // индекс поиска \t \t \t \t \t \t \t \t \t эхо $ индекса; \t \t \t \t \t \t \t \t \t, если (array_key_exists ($ индекс, $ обр)) \t \t \t \t \t \t \t \t \t { \t \t \t \t \t \t \t \t \t \t снята с охраны ($ обр [$ индекс ]); // удалить индекс массива \t \t \t \t \t \t \t \t \t} –

Смежные вопросы