Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active October 26, 2020 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorbenic/4583131bcef1ce7c07dbd088605744b6 to your computer and use it in GitHub Desktop.
Save igorbenic/4583131bcef1ce7c07dbd088605744b6 to your computer and use it in GitHub Desktop.
Programmatically Upload and Unpack ZIP files in WordPress | on https://www.ibenic.com/
<?php
/**
* Registering our shortcode
*/
function ibenic_register_upload_form() {
add_shortcode( 'zip_upload_form', 'ibenic_zip_upload_form' );
}
/**
* The ZIP upload form.
*
* @param array $atts Shortcode Array
*/
function ibenic_zip_upload_form( $atts ) {
ob_start();
?>
<form method="POST" action="" enctype="multipart/form-data">
<?php
wp_nonce_field( 'zip_upload_nonce', 'zip_upload_nonce' );
?>
<input type="file" accept="application/zip" name="uplaod" />
<button class="submit button" name="upload_file" type="submit">Upload</button>
</form>
<?php
return ob_get_clean();
}
<?php
/**
* Plugin Name: Upload ZIP Form
* Description: Have a form that will upload ZIP files and unpack them.
* Author: Igor Benić
* Author URI: https://ibenic.com
*/
if ( ! defined( 'ABSPATH' ) ) {
return;
}
class Upload_ZIP_Form {
protected $errors = null;
protected $notices = [];
public function __construct() {
add_action( 'init', [ $this, 'register' ] );
add_action( 'init', [ $this, 'upload' ] );
$this->errors = new WP_Error();
}
public function register() {
add_shortcode( 'zip_upload_form', [ $this, 'form' ] );
}
}
new Upload_ZIP_Form();
<?php
/**
* Plugin Name: Upload ZIP Form
* Description: Have a form that will upload ZIP files and unpack them.
* Author: Igor Benić
* Author URI: https://ibenic.com
*/
if ( ! defined( 'ABSPATH' ) ) {
return;
}
class Upload_ZIP_Form {
// ...
public function form( $atts ) {
ob_start();
if ( $this->notices ) {
?>
<ul>
<?php
foreach( $this->notices as $notice ) {
?>
<li><?php echo $notice; ?></li>
<?php
}
?>
</ul>
<?php
}
if ( $this->errors->get_error_messages() ) {
?>
<ul>
<?php
foreach( $this->errors->get_error_messages() as $message ) {
?>
<li><?php echo $message; ?></li>
<?php
}
?>
</ul>
<?php
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<?php
wp_nonce_field( 'zip_upload_nonce', 'zip_upload_nonce' );
?>
<input type="file" accept="application/zip" name="file" />
<button class="submit button" name="upload_file" type="submit">Upload</button>
</form>
<?php
return ob_get_clean();
}
}
new Upload_ZIP_Form();
<?php
/**
* Plugin Name: Upload ZIP Form
* Description: Have a form that will upload ZIP files and unpack them.
* Author: Igor Benić
* Author URI: https://ibenic.com
*/
if ( ! defined( 'ABSPATH' ) ) {
return;
}
class Upload_ZIP_Form {
// ....
public function upload() {
if ( ! isset( $_POST['zip_upload_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['zip_upload_nonce'], 'zip_upload_nonce' ) ) {
return;
}
$posted_data = isset( $_POST ) ? $_POST : array();
$file_data = isset( $_FILES ) ? $_FILES : array();
$data = array_merge( $posted_data, $file_data );
include_once 'includes/class-zip-uploader.php';
$uploader = new ZIP_Uploader( 'zips' );
$result = $uploader->upload($data );
if ( is_wp_error( $result ) ) {
$this->errors->add( $result->get_error_code(), $result->get_error_message() );
}
$this->notices[] = 'Uploaded! Path: ' . $result;
}
}
new Upload_ZIP_Form();
<?php
/**
* Plugin Name: Upload ZIP Form
* Description: Have a form that will upload ZIP files and unpack them.
* Author: Igor Benić
* Author URI: https://ibenic.com
*/
if ( ! defined( 'ABSPATH' ) ) {
return;
}
class Upload_ZIP_Form {
protected $errors = null;
protected $notices = [];
public function __construct() {
add_action( 'init', [ $this, 'register' ] );
add_action( 'init', [ $this, 'upload' ] );
$this->errors = new WP_Error();
}
public function register() {
add_shortcode( 'zip_upload_form', [ $this, 'form' ] );
}
public function form( $atts ) {
ob_start();
if ( $this->notices ) {
?>
<ul>
<?php
foreach( $this->notices as $notice ) {
?>
<li><?php echo $notice; ?></li>
<?php
}
?>
</ul>
<?php
}
if ( $this->errors->get_error_messages() ) {
?>
<ul>
<?php
foreach( $this->errors->get_error_messages() as $message ) {
?>
<li><?php echo $message; ?></li>
<?php
}
?>
</ul>
<?php
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<?php
wp_nonce_field( 'zip_upload_nonce', 'zip_upload_nonce' );
?>
<input type="file" accept="application/zip" name="file" />
<button class="submit button" name="upload_file" type="submit">Upload</button>
</form>
<?php
return ob_get_clean();
}
public function upload() {
if ( ! isset( $_POST['zip_upload_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['zip_upload_nonce'], 'zip_upload_nonce' ) ) {
return;
}
$posted_data = isset( $_POST ) ? $_POST : array();
$file_data = isset( $_FILES ) ? $_FILES : array();
$data = array_merge( $posted_data, $file_data );
include_once 'includes/class-zip-uploader.php';
$uploader = new ZIP_Uploader( 'zips' );
$result = $uploader->upload($data );
if ( is_wp_error( $result ) ) {
$this->errors->add( $result->get_error_code(), $result->get_error_message() );
}
$this->notices[] = 'Uploaded! Path: ' . $result;
}
}
new Upload_ZIP_Form();
<?php
class ZIP_Uploader {
protected $folder = '';
public function __construct( $folder ) {
$this->folder = $folder;
}
/**
* Get folder name where to upload
*
* @param $post_id
*
* @return string
*/
public function get_folder_name( $filename ) {
return sanitize_title( $filename );
}
/**
* Get target path for the parent folder where all files are uploaded
*
* @return string
*/
public function get_target_path() {
$upload_directory = wp_get_upload_dir();
$upload_baseurl = $upload_directory['basedir'];
return trailingslashit( $upload_baseurl ) . $this->folder;
}
/**
* Get path
*
* @param $post_id
*
* @return string
*/
public function get_folder_path( $folder ) {
return trailingslashit( $this->get_target_path() ) . $folder;
}
/**
* Check if there is an error
*
* @param $error
*
* @return bool|WP_Error
*/
public function check_error($error) {
$file_errors = array(
0 => __( "There is no error, the file uploaded with success", 'your_textdomain' ),
1 => __( "The uploaded file exceeds the upload_max_files in server settings", 'your_textdomain' ),
2 => __( "The uploaded file exceeds the MAX_FILE_SIZE from html form", 'your_textdomain' ),
3 => __( "The uploaded file uploaded only partially", 'your_textdomain' ),
4 => __( "No file was uploaded", 'your_textdomain' ),
6 => __( "Missing a temporary folder", 'your_textdomain' ),
7 => __( "Failed to write file to disk", 'your_textdomain' ),
8 => __( "A PHP extension stoped file to upload", 'your_textdomain' ),
);
if ( $error > 0 ) {
return new \WP_Error( 'file-error', $file_errors[ $error ] );
}
return true;
}
}
<?php
class ZIP_Uploader {
// ...
/**
* Upload File
*
* @param $file
*
* @return bool|string|true|WP_Error
*/
public function upload( $file ) {
/** @var $wp_filesystem \WP_Filesystem_Direct */
global $wp_filesystem;
if ( ! function_exists( 'WP_Filesystem' ) ) {
include_once 'wp-admin/includes/file.php';
}
WP_Filesystem();
$file_error = $file["file"]["error"];
// Check for Errors
if ( is_wp_error( $this->check_error( $file_error ) ) ) {
return $this->check_error( $file_error );
}
$file_name = $file["file"]["name"];
$file_name_arr = explode( '.', $file_name );
$extension = array_pop( $file_name_arr );
$filename = implode( '.', $file_name_arr ); // File Name
$zip_file = sanitize_title( $filename ) . '.' . $extension; //Our File
if ( 'zip' !== $extension ) {
return new WP_Error( 'no-zip', 'This does not seem to be a ZIP file' );
}
$temp_name = $file["file"]["tmp_name"];
$file_size = $file["file"]["size"];
// Get our destination folder
$current_folder = $this->get_folder_path( $this->get_folder_name( $filename ) );
// Get default folder that contains all zips. Create if does not exists.
$default_target = $this->get_target_path();
// Create our default folder if it does not exist
if( ! file_exists( $default_target ) ){
mkdir( $default_target );
}
// Get folder path
$upload_path = $this->get_folder_path( $this->get_folder_name( $filename ) );
// We will overwrite it all, so remove it.
if ( $wp_filesystem->exists( $upload_path ) ) {
$wp_filesystem->delete( $upload_path, true );
}
// Create it
if ( ! $wp_filesystem->exists( $upload_path ) ) {
$wp_filesystem->mkdir( $upload_path );
}
// Folder name where we will upload the ZIP
$working_dir = $upload_path . '-zip';
// Delete if such folder exists
if ( $wp_filesystem->is_dir( $working_dir ) ) {
$wp_filesystem->delete( $working_dir, true );
}
// Create the folder to hold our zip file
$wp_filesystem->mkdir( $working_dir );
// Uploading ZIP file
if( move_uploaded_file( $temp_name, $working_dir . "/" . $zip_file ) ){
// Unzip the file to the upload path
$unzip_result = unzip_file( $working_dir . "/" . $zip_file, $upload_path );
if ( is_wp_error( $unzip_result ) ) {
return $unzip_result;
} else {
// No errors with unzips, let's delete everything and unzip it again.
if ( $wp_filesystem->is_dir( $upload_path ) ) {
$wp_filesystem->delete( $upload_path, true );
}
$wp_filesystem->mkdir( $upload_path );
unzip_file( $working_dir . "/" . $zip_file, $upload_path );
}
// Remove the uploaded zip
@unlink( $working_dir . "/" . $zip_file );
if ( $wp_filesystem->is_dir( $working_dir ) ) {
$wp_filesystem->delete( $working_dir, true );
}
return $upload_path;
} else {
return new \WP_Error( 'not-uploaded', __( 'Could not upload file', 'your_textdomain' ) );
}
}
}
<?php
class ZIP_Uploader {
protected $folder = '';
public function __construct( $folder ) {
$this->folder = $folder;
}
/**
* Get folder name where to upload
*
* @param $post_id
*
* @return string
*/
public function get_folder_name( $filename ) {
return sanitize_title( $filename );
}
/**
* Get target path for the parent folder where all files are uploaded
*
* @return string
*/
public function get_target_path() {
$upload_directory = wp_get_upload_dir();
$upload_baseurl = $upload_directory['basedir'];
return trailingslashit( $upload_baseurl ) . $this->folder;
}
/**
* Get path
*
* @param $post_id
*
* @return string
*/
public function get_folder_path( $folder ) {
return trailingslashit( $this->get_target_path() ) . $folder;
}
/**
* Check if there is an error
*
* @param $error
*
* @return bool|WP_Error
*/
public function check_error($error) {
$file_errors = array(
0 => __( "There is no error, the file uploaded with success", 'bam_courses_launcher' ),
1 => __( "The uploaded file exceeds the upload_max_files in server settings", 'bam_courses_launcher' ),
2 => __( "The uploaded file exceeds the MAX_FILE_SIZE from html form", 'bam_courses_launcher' ),
3 => __( "The uploaded file uploaded only partially", 'bam_courses_launcher' ),
4 => __( "No file was uploaded", 'bam_courses_launcher' ),
6 => __( "Missing a temporary folder", 'bam_courses_launcher' ),
7 => __( "Failed to write file to disk", 'bam_courses_launcher' ),
8 => __( "A PHP extension stoped file to upload", 'bam_courses_launcher' ),
);
if ( $error > 0 ) {
return new \WP_Error( 'file-error', $file_errors[ $error ] );
}
return true;
}
/**
* Upload File
*
* @param $file
*
* @return bool|string|true|WP_Error
*/
public function upload( $file ) {
/** @var $wp_filesystem \WP_Filesystem_Direct */
global $wp_filesystem;
if ( ! function_exists( 'WP_Filesystem' ) ) {
include_once 'wp-admin/includes/file.php';
}
WP_Filesystem();
$file_error = $file["file"]["error"];
// Check for Errors
if ( is_wp_error( $this->check_error( $file_error ) ) ) {
return $this->check_error( $file_error );
}
$file_name = $file["file"]["name"];
$file_name_arr = explode( '.', $file_name );
$extension = array_pop( $file_name_arr );
$filename = implode( '.', $file_name_arr );
$zip_file = sanitize_title( $filename ) . '.' . $extension;
if ( 'zip' !== $extension ) {
return new WP_Error( 'no-zip', 'This does not seem to be a ZIP file' );
}
$temp_name = $file["file"]["tmp_name"];
$file_size = $file["file"]["size"];
$current_folder = $this->get_folder_path( $this->get_folder_name( $filename ) );
// Get default folder that contains all courses. Create if does not exists.
$default_target = $this->get_target_path();
if( ! file_exists( $default_target ) ){
mkdir( $default_target );
}
// Get course folder path, create if not exists
$upload_path = $this->get_folder_path( $this->get_folder_name( $filename ) );
if ( $wp_filesystem->exists( $upload_path ) ) {
$wp_filesystem->delete( $upload_path, true );
}
if ( ! $wp_filesystem->exists( $upload_path ) ) {
$wp_filesystem->mkdir( $upload_path );
}
// Getting a folder where we will upload the ZIP
$working_dir = $upload_path . '-zip';
if ( $wp_filesystem->is_dir( $working_dir ) ) {
$wp_filesystem->delete( $working_dir, true );
}
$wp_filesystem->mkdir( $working_dir );
// Uploading ZIP file
if( move_uploaded_file( $temp_name, $working_dir . "/" . $zip_file ) ){
// Unzip the file to the upload path
$unzip_result = unzip_file( $working_dir . "/" . $zip_file, $upload_path );
if ( is_wp_error( $unzip_result ) ) {
return $unzip_result;
} else {
// No errors with unzips, let's delete everything and unzip it again.
if ( $wp_filesystem->is_dir( $upload_path ) ) {
$wp_filesystem->delete( $upload_path, true );
}
$wp_filesystem->mkdir( $upload_path );
unzip_file( $working_dir . "/" . $zip_file, $upload_path );
}
// Remove the uploaded zip
@unlink( $working_dir . "/" . $zip_file );
if ( $wp_filesystem->is_dir( $working_dir ) ) {
$wp_filesystem->delete( $working_dir, true );
}
return $upload_path;
} else {
return new \WP_Error( 'not-uploaded', __( 'Could not upload file', 'your_textdomain' ) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment