Wordpress使用文件删除metabox上下载的图像

时间:2016-08-30 23:48:46

标签: wordpress file

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。

数据库的输出类似于我在下面显示的内容

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>

如果你帮助我,我感激不尽

1 个答案:

答案 0 :(得分: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