解压缩php中的文件...文件名问题

时间:2017-05-06 09:29:54

标签: php wordpress

我在wordpress中整理了一个主题,我需要能够上传包含html横幅,资源和脚本的zip文件。

我需要一些人为我写的功能提供一些帮助,这种功能时不时地起作用。基本上它用于获取zip文件,解压缩并使用相同的文件名对文件夹内的所有文件进行充气。这就是我所拥有的:

if (is_admin()) {
    add_action('wp_ajax_zip_to_html', 'zip_to_html');
}
function zip_to_html()
{
    require_once(ABSPATH .'/wp-admin/includes/file.php');
    $zippedfile         = $_POST['zippedfile'];
    WP_Filesystem();
    $destination        = wp_upload_dir();
    $base_path          = $destination['basedir'];
    $upload_folder_name = end(explode('/', $base_path));
    $zippedfile_filename= end(explode('/', $zippedfile));
    $zippedfile_subtem  = end(explode($upload_folder_name, $zippedfile));
    $zippedfile_subdir  = current(explode($zippedfile_filename, $zippedfile_subtem));
    $destination_path   = $base_path.''.$zippedfile_subdir;

    $uploadedfolder_link= current(explode($upload_folder_name, $zippedfile));
    $unzipped_folder    = pathinfo($zippedfile_filename)[ 'filename' ];
    $unzipped_folder    = current(explode('-', $unzipped_folder));


    $zip = new ZipArchive;
    $res = $zip->open($destination_path.'/'.$zippedfile_filename);
    if ($res === true) {
        //$zip->renameIndex(0,'new');
        $zip->extractTo($destination_path.''.$unzipped_folder);
        $innerfolder  = $unzipped_folder.'/';
        $innerfolder .= $zip->getNameIndex(0);
        $zip->close();
    } else {
        $return = array(
            'link'  => 'Zip is not decrepted!!',
            'er'    => ''
        );
        wp_send_json($return);
    }



    $destination_url = $uploadedfolder_link.''.$upload_folder_name.''.$zippedfile_subdir.''.$innerfolder;

    $index_file = $destination_path.''.$innerfolder.'index.html';

   //echo $unzipfile;
    if ($res === true) {
        $doc = new DOMDocument();
        $doc->loadHTMLFile($index_file);
        $tags = $doc->getElementsByTagName('img');
        foreach ($tags as $tag) {
            $old_src = $tag->getAttribute('src');
            if (!file_exists($old_src)) {
                $new_src_url = $destination_url.''.$old_src;
                $new_src_path = $base_path.$zippedfile_subdir.''.$innerfolder.''.$old_src;
                //echo $new_src_path;
                if (file_exists($new_src_path)) {
                    $tag->setAttribute('src', $new_src_url);
                    $tag->setAttribute('src', $new_src_url);
                    $doc->saveHTML($tag);
                }
            }
        }
        $links = $doc->getElementsByTagName('link');
        foreach ($links as $link) {
            $old_src = $link->getAttribute('href');
            if (!file_exists($old_src)) {
                $new_src_url = $destination_url.''.$old_src;
                $new_src_path = $base_path.$zippedfile_subdir.''.$innerfolder.''.$old_src;
                //echo $new_src_path;
                if (file_exists($new_src_path)) {
                    $link->setAttribute('href', $new_src_url);
                    $link->setAttribute('href', $new_src_url);
                    $doc->saveHTML($link);
                }
            }
        }
        $doc->saveHTML();
        $doc->saveHTMLFile($index_file);
    } else {
        echo 'There was an error unzipping the file.';
    }
    $return = array(
        'link'  => $destination_url.'index.html',
        'er'    => ''
    );
    wp_send_json($return);
}

add_action('admin_footer', 'my_action_javascript'); // Write our JS below here

function my_action_javascript()
{
    ?>
    <script type="text/javascript" >
    jQuery(document).ready(function($) {
        $( "input[name='banner_meta_tag_width'], input[name='banner_meta_tag_height']" ).change(function() {
            var NsWidthInput = $( "#banner_meta_tag_width" ).val();
            var NsHeightInput = $( "#banner_meta_tag_height" ).val();
            $(".banner_preview iframe").attr("width", NsWidthInput);
            $(".banner_preview iframe").attr("height", NsHeightInput);
            var html = $(".banner_preview").html();
            $( "input[name='banner_meta_tag']" ).val(html);
        });
    });
    </script>
    <?php
}

我遇到的问题,我找不到如何重现它(直到现在完全随机)是创建一个html文件连接zip中的两个文件名。看图像 inflated files

出于某种原因,我得到了第二个html文件,当在zip内部时只有index.html

有什么想法吗?

0 个答案:

没有答案