Friday, September 23, 2022
HomeWordPress Developmentarray - The way to get recordsdata from loop for zip

array – The way to get recordsdata from loop for zip


I’m engaged on a button which is able to obtain the pictures as a zipper on particular posts. It really works if I go away the recordsdata array in capabilities.php with a number of hard-coded img hyperlinks. However once I transfer the array to the customized publish the place the pictures I must zip are already being looped and displayed, I can nonetheless obtain through button, however opening the downloaded zip I get error “Unable to develop archive. It’s in an unsupported format” "Unable to expand archive. It is in an unsupported format

I’ve tried transferring totally different elements of the code forwards and backwards, like all of the code within the customized publish, and all of the code in capabilities.php, to no avail. I should be not getting the img file urls correctly however I am undecided how.

That is the entire talked about code, solely in capabilities.php (besides the obtain button), with the hard-coded picture hyperlinks, which works as supposed:

$image1 = 'https://com.tripletigers/wp-content/uploads/homesweet_nowords-1920x1920.jpg';
$image2 = 'https://com.tripletigers/wp-content/uploads/V3-1920x2880.jpg';
$image3 = 'https://com.tripletigers/wp-content/uploads/SM_Single-Cover_Damn-Strait-1920x1920.jpg';

$recordsdata = array($image1, $image2, $image3);


$tmpFile = tempnam('/tmp', '');

$zip = new ZipArchive;
$zip->open($tmpFile, ZipArchive::CREATE);
foreach ($recordsdata as $file) {
    // obtain file
    $fileContent = file_get_contents($file);

    $zip->addFromString(basename($file), $fileContent);
}
$zip->shut();

// Obtain Created Zip file from button through type submit
if(isset($_POST['download'])){

    header('Content material-Kind: utility/zip');
    header('Content material-disposition: attachment; filename=artist-photos.zip');
    header('Content material-Size: ' . filesize($tmpFile));
    readfile($tmpFile);

    unlink($tmpFile);
}

That is what results in the error, making an attempt to get the pictures from the in any other case working loop within the customized publish:

foreach( $list_posts as $list_item ) :
    if ( $list_item->post_type == 'crb_photo' ):
        if ( has_post_thumbnail( $list_item->ID ) ) {
            $img_url = get_the_post_thumbnail_url( $list_item->ID, 'crb_full_width' );              
             // this var i created for the zip, so as to add every picture url to the array
            $getImage =  esc_url($img_url);
            // testing: prints picture url. Why is that url not attending to the zip?
            echo '<h6>' . $img_url . '</h6>';
            
            // create array for zip - tried each = and .=   
            $recordsdata[] .= $getImage;
            // similar outcome as above
            // $recordsdata[] = $getImage;
        } ?>
                            
            <li>
                <div>
                   <?php if ( ! empty( $img_url ) ) : ?>
                        <a href="<?php echo esc_url( $img_url ); ?>"></a>
                   <?php endif; ?>
                </div>
            </li>
    <?php endif; ?>

The capabilities.php used with above (similar as first snippet however with out hard-coded array)

$tmpFile = tempnam('/tmp', '');

$zip = new ZipArchive;
$zip->open($tmpFile, ZipArchive::CREATE);
foreach ($recordsdata as $file) {
    // obtain file
    $fileContent = file_get_contents($file);

    $zip->addFromString(basename($file), $fileContent);
}
$zip->shut();

// Obtain Created Zip file
if(isset($_POST['download'])){

    header('Content material-Kind: utility/zip');
    header('Content material-disposition: attachment; filename=artist-photos.zip');
    header('Content material-Size: ' . filesize($tmpFile));
    readfile($tmpFile);

    unlink($tmpFile);
}

When making an attempt to open zip through command line:

When trying to open zip via command line

What am i lacking so as to get these pictures into the zip correctly?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments