Using unzip_file() to get contents of file
With each post, I save a zip folder as an attachment containing a json file with large amount of data. When looping through posts, I need to open this folder and retrieve the contents of json file but I can't get this to work.
I'm running my wordpress installation on xampp and I've made sure that all users have full control of the xampp folder and subfolders
global $post;
global $wp_filesystem;
while ( $messages-have_posts() ) :
$messages-the_post();
$pid = get_the_ID();
$fileExt = ".json";
$folderExt = ".zip";
$msgData = get_attached_media( 'application', $pid );
$msgData = array_values($msgData);
$attachID = $msgData[0]-ID;
$dataTitle = $msgData[0]-post_title;
$fileName = $dataTitle . $fileExt;
$folderName = $dataTitle . $folderExt;
$folder = $msgData[0]-guid;
WP_Filesystem();
$destination = wp_upload_dir();
$destination_path = $destination['path'];
$unzipfile = unzip_file( $destination_path .'/'. $fileName, $destination_path);
if ( $unzipfile ) {
echo 'Successfully unzipped the file!';
var_dump($unzipfile);
$msgDataContents = file_get_contents($folder .'/'. $fileName);
var_dump($msgDataContents);
} else {
echo 'There was an error unzipping the file.';
}
The name of the zip and json file are the same.
I have confirmed that the $folder returns the correct url of the zip going to that url downloads the zip containing a json file with the correct content.
I think I'm making mistakes with paths but I've tried many. The first var_dump returns bool(true) and the second returns bool(false). What is the correct way to do this?
Topic ziparchive uploads attachments Wordpress
Category Web