最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

uploads - Using unzip_file() to get contents of file

matteradmin6PV0评论

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?

Post a comment

comment list (0)

  1. No comments so far