unzipping files in php … filename issue

I’m putting together a theme in wordpress, and I need to be able to upload zip files containing html banners, their assets and scripts.

I need a bit of help with a function someone wrote for me, that works every now and then. Basically it is used to grab the zip file, unzip it and inflate all files inside a folder with the same filename. Here goes what I have:

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
}

The problem I’m experiencing and I cannot find find how to reproduce it (totally random until now) is that creates a html file concatenating two of the filenames inside the zip. See the image
inflated files

for some reason I get a second html file, when inside the zip there is only index.html

Any ideas?

Read more here: unzipping files in php … filename issue

Leave a Reply

Your email address will not be published. Required fields are marked *