admin管理员组文章数量:1025457
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?
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?
本文标签: uploadsUsing unzipfile() to get contents of file
版权声明:本文标题:uploads - Using unzip_file() to get contents of file 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745632208a2160233.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论