unzip_file not working with the remote file

I'm trying to use unzip_file to extract the remote archive with no success. It works only with the local path using get_template_directory() but with a remote zip archive URL I'm getting Incompatible Archive. message. Here is the stripped version of my code: WP_Filesystem(); global $wp_filesystem; $source = 'http://downloads.wordpress.org/theme/ona-creative.1.0.0.zip'; $unzipfile = unzip_file( $source, get_theme_root() ); if ( is_wp_error( $unzipfile ) ) { wp_send_json( array( 'done' => 1, 'message' => esc_html__( $unzipfile->get_error_message() . ' There was an error unzipping the file.', 'ona' …
Category: Web

Wordpress can't use ZipArchive

Im currently developing a site where customers can download brand images like packshots. Ive made a custom cart where the client should be able to download all images added to the cart using ZipArchive. But it dosent seem to work as easy if it was not WordPress(as it usually is). My code: $files = array('test.jpg', 'test2.jpg'); $zipname = 'images.zip'; $zip = new ZipArchive; $zip->open($zipname, ZipArchive::CREATE); foreach ($files as $file) { $zip->addFile($file); } $zip->close(); header('Content-Type: application/zip'); header('Content-disposition: attachment; filename='.$zipname); header('Content-Length: ' …
Category: Web

How to prepare (compress/zip) a plugin to enable updating instead of adding new instance?

I created my own plugins and install/update them by uploading the zip file in the dashboard. That worked well, but suddenly1 a new instance of the plugin gets added instead of updating the already installed one. Before WP would ask "do you want to replace version X with version Y", but that dialog does not appear anymore. I had this issue before, but back then I had the date and version in the name of the zip file. Ever since …
Category: Web

How do you use unzip_file()?

I have been trying to use the unzip_file() function. It says undefined so I looked into it and the WP_Filesystem() must be called and set up. So easy, right? require_once(ABSPATH .'/wp-admin/includes/file.php'); WP_Filesystem(); unzip_file( $zip, $dest ); Even this shows as undefined, and I don't see any documentation on it. I am trying to use the unzip feature to unpack a separate plugin during theme install.
Category: Web

Upload Wordpress Plugin from ZIP Broken - Returns Symbols Squares & question marks

I am trying to upload plugins to my wordpress website. However, when I click "Install Now" my wordpress reloads and prints out a long string of symbols like this: #�EU퇘0'��������q=�y����������{��V�㝀�P(�-���=�p=�%'G�dB���߯��_�����a���k ��� bB#�t!81Q�j@H��L˷֝q&TvR���Q� �����������{3W7�9�<�u����g�:��������.�d|�L$EY�Y��SF� �)T*�2R�ɮ�4C�! Everything else on my site works fine AND I can add plugins by searching and clicking "Install Now." I guess there's a corrupted file? I have literally no clue and couldn't find anything by googling. Server details: Wordpress 5.5 Hosted on Siteground with Cloudflare Deactivated all …
Category: Web

How to assign multiple file-mime-types to extension?

To allow extra file types for upload, we use: add_filter('mime_types', 'my_mimes'); function my_mimes($all) { $all['zip'] = 'application/zip'; return $all; } But how to assign multiple file types to 1 extension? For example, zip extension might have application/zip mime and also application/x-zip (like, coming from 7zip). This way doesn't work: $all['zip'] = ['application/zip', 'application/x-zip'] ;
Category: Web

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(); …
Category: Web

Zip a file and add as attachment

I first add support for mime type attachments // ADD MIME TYPEs FOR ATTACHMENTS add_filter( 'upload_mimes', 'my_myme_types', 1, 1 ); function my_myme_types( $mime_types ) { $mime_types['txt'] = 'text/plain'; $mime_types['json'] = 'application/json'; $mime_types['zip'] = 'application/zip'; return $mime_types; } Now I have the way that I would add a json object to to a json file and then attach $user_id = absint($_POST["uid"]); $post_title = sanitize_text_field($_POST["post_title"]); $post_object = get_page_by_title($post_title, OBJECT, 'chat'); $post_id = $post_object->ID; $msg_array = $_POST["msg_data"]; $ext = ".json"; $path = "/wp/wp-content/uploads/"; …
Category: Web

Can a manually uploaded plugin be made to track updates from the WordPress.org plugin directory?

I've made a plugin which I will give to some users to test by manually uploading the zip file. I later on want to host the plugin in the WordPress.org plugin directory, but I want to avoid users having to uninstall my plugin to install the directory-hosted one because I have code in my uninstaller that deletes all of the terms, etc. that my plugin adds. Is there a way to make WordPress track a plugin in the official directory …
Category: Web

Download a zip folder of selected files

I'm trying to download multiple files from the server in a zipped folder. Here is the code I have thus far: HTML <form method="post"> <input type="checkbox" name="checked[]" value="<?php echo $path; ?>"> <input type="submit" name="download" value="Download Selected"> </form> PHP: if(isset($_POST['download'])){ $files = $_POST['checked']; foreach ($files as $file ) { if(file_exists($_SERVER['DOCUMENT_ROOT'].'/upload/folder/'. $file)){ require_once(ABSPATH . "wp-admin" . '/includes/file.php'); $full_path = $_SERVER['DOCUMENT_ROOT'].'/bcg/wp-content/uploads/'. $file; download_url($full_path); } else { echo 'file doesnt exist'; } } }
Category: Web

Why does unzip_file always return true but nothing happens?

I am trying to use the $wp_filesystem of WordPress to unzip a file located inside the wp-content/plugins and place it at the same location. However whatever I try the WordPress method unzip_file() always returns true as if it worked (but no file appeared on the server). This is the code I have for unzipping the file: WP_Filesystem(); $zip_from = get_home_path().'wp-content/plugins/'.$filename; $zip_to = str_replace(ABSPATH, $wp_filesystem->abspath(), get_home_path().'wp-content/plugins/'); if(!unzip_file($zip_from,$zip_to)) { return new \WP_Error('writing_error', 'Couldn\'t extract the plugin\'s ZIP file.'); } else exit("worked"); So …
Category: Web

How do you protect a premium theme from being copied?

I'm thinking of doing up a couple of premium themes, and selling copies of them. I've never done anything like this before, so I have a couple of questions. I appreciate that this is mainly a forum for asking technical questions, so if anyone wants to point me in the direction of somewhere that might be more appropriate for these questions, please let me know. My main question is about copies of a theme. If I sell a copy of …
Category: Web

How can I fix my server so core/plugins/theme update don't silently fail?

I run my own server and have a few dozen WordPress sites. Recently, many updates (plugin and core) have been silently failing. If I try to do the update from the updates page, I get a message like this one: The update process is starting. This process may take a while on some hosts, so please be patient. Enabling Maintenance mode… Updating Plugin Jetpack by WordPress.com (1/1) But it never progresses further. If I attempt the update from the plugins …
Category: Web

AJAX issue - Uncaught SyntaxError when processing Zip File

I'm creating an application that will allow a user to fill out a form and then have a customized zip file downloaded for them upon submit. I'm utilizing AJAX to accomplish this task. Build.prototype.ajaxHandler = function(method, values) { jQuery.ajax({ type: 'POST', url: WP_LOCALIZED['url'], data: { action: 'request_handler', method: method, data: values }, success: function(data) { var response = jQuery.parseJSON(data); console.log(response); }, error: function(XMLHttpRequest, textStatus, errorThrown) { console.log(XMLHttpRequest, textStatus, errorThrown); } }); }; Whenever I try to return basic data with …
Category: Web

Duplicating wordpress install issue

I wonder if anyone could help me with a problem that I am having. I am having a guy do some plugin/theme customization on my wordpress site and he wants to have a duplicate of the site to work with on localhost. I have sent the mysql database and a copy of the wordpress site in a folder to him or suggested he just work on our 'live' site to get this response. I was wondering whether anyone knew what …
Category: Web

What does the output of this plugin mean?

I just tried to compress a file and am not sure if it worked or not. The plugin I am using gave me the following results: adding: wp-includes/js/tinymce/plugins/wpeditimage/editimage.html (deflated 76%) adding: wp-includes/js/tinymce/plugins/wpeditimage/php.ini (deflated 64%) adding: wp-includes/js/tinymce/plugins/wpdialogs/ (stored 0%) The line I am concerned about is the third one: wp-includes/js/tinymce/plugins/wpdialogs/ (stored 0%) Can anyone explain what this means? I think this could mean: The file was not able to compress OR The file was not able to get stored in part …
Category: Web

What should I pass for $needed_dirs when calling _unzip_file_pclzip (aka PclZip)?

I'm trying to create a fallback method when ZipArchive is not present. I'm seeking to use the _unzip_file_pclzip() function contained in wp-admin/includes/file.php However, I'm not sure what is expected for the $needed_dirs argument. My target folder for the zip will be the "styles" folder under my theme folder, so the path would be "wp-content/themes/mytheme/styles/" Is that what's expected for $needed_dirs? function openZip($file_to_open) { global $wp_filesystem;//required if _unzip_file_pclzip is used directly global $target; if(class_exists('ZipArchive')) { $zip = new ZipArchive(); $x = …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.