Tuxedo Big File Uploads - Version 1.0

Version Description

  • Initial release.
Download this release

Release Info

Developer andtrev
Plugin Icon 128x128 Tuxedo Big File Uploads
Version 1.0
Comparing to
See all releases

Version 1.0

Files changed (4) hide show
  1. readme.txt +51 -0
  2. tux_handle_upload.php +88 -0
  3. tuxedo_big_file_uploads.php +208 -0
  4. uninstall.php +18 -0
readme.txt ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Tuxedo Big File Uploads ===
2
+ Contributors: andtrev
3
+ Tags: AJAX, file uploader, files, files uploader, ftp, image uploader, plugin, upload
4
+ Requires at least: 3.4
5
+ Tested up to: 4.4.1
6
+ Stable tag: 1.0
7
+ License: GPLv2 or later
8
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
+
10
+ Enables large file uploads in the built-in WordPress media uploader.
11
+
12
+ == Description ==
13
+
14
+ Enables large file uploads in the standard built-in WordPress media uploader. Uploads can be as large as available disk space allows.
15
+
16
+ No messing with initialization files or settings. No need to FTP.
17
+
18
+ Simply activate the plugin and use the media uploader as you normally would.
19
+
20
+ The browser uploader option is not supported, the multi-file uploader must be used to enable large file uploads.
21
+
22
+ * Small footprint that doesn't bog down WordPress with unnecessary features.
23
+ * Shows available disk space for temporary uploads directory as maximum upload file size in media uploader.
24
+ * Options for chunk size and max retries are available under the Uploading Files section on the Settings -> Media page.
25
+
26
+ In essence the plugin changes the Plupload settings for uploads and points the AJAX url to the plugin. This processes the
27
+ upload in chunks (separate smaller pieces) before handing it off to the original AJAX url (WordPress).
28
+
29
+ == Installation ==
30
+
31
+ 1. Upload the plugin files to the `/wp-content/plugins/tuxedo-big-file-uploads` directory, or install the plugin through the WordPress plugins screen directly.
32
+ 2. Activate the plugin through the 'Plugins' screen in WordPress.
33
+ 3. Use the Settings -> Media screen to configure the plugin.
34
+
35
+ == Frequently Asked Questions ==
36
+
37
+ = How big can uploads be? =
38
+
39
+ Uploads can be as large as available disk space for temporary files allows.
40
+
41
+ = Why does the maximum file size decrease after an upload? =
42
+
43
+ Maximum file size is listed as the available free disk space for temporary uploads.
44
+ Free disk space will decrease as files are uploaded.
45
+ Additionally some systems use a separate partition for temporary files, free space may fluctuate as files
46
+ are uploaded and moved out of the temporary folder.
47
+
48
+ == Changelog ==
49
+
50
+ = 1.0 =
51
+ * Initial release.
tux_handle_upload.php ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * TuxedoBigFileUploads Handle Upload
4
+ *
5
+ * Ajax callback for plupload to handle chunked uploads.
6
+ *
7
+ * @package TuxedoBigFileUploads
8
+ * @since 1.0.0
9
+ *
10
+ * Based on code by Davit Barbakadze
11
+ * https://gist.github.com/jayarjo/5846636
12
+ */
13
+
14
+ /** Check that we have an upload and there are no errors. */
15
+ if ( empty( $_FILES ) || $_FILES['async-upload']['error'] ) {
16
+ /** Failed to move uploaded file. */
17
+ die();
18
+ }
19
+
20
+ if ( ! function_exists( 'mime_content_type' ) ) {
21
+ /**
22
+ * Return a file's mime type.
23
+ *
24
+ * @since 1.0.0
25
+ *
26
+ * @param string $filename File name.
27
+ * @return var string $mimetype Mime type.
28
+ */
29
+ function mime_content_type( $filename ) {
30
+
31
+ $finfo = finfo_open( FILEINFO_MIME );
32
+ $mimetype = finfo_file( $finfo, $filename );
33
+ finfo_close( $finfo );
34
+ return $mimetype;
35
+
36
+ }
37
+ }
38
+
39
+ /** Check and get file chunks. */
40
+ $chunk = isset( $_REQUEST['chunk']) ? intval( $_REQUEST['chunk'] ) : 0;
41
+ $chunks = isset( $_REQUEST['chunks']) ? intval( $_REQUEST['chunks'] ) : 0;
42
+
43
+ /** Get file name and path + name. */
44
+ $fileName = isset( $_REQUEST['name'] ) ? $_REQUEST['name'] : $_FILES['async-upload']['name'];
45
+ $filePath = dirname( $_FILES['async-upload']['tmp_name'] ) . '/' . md5( $fileName );
46
+
47
+ /** Open temp file. */
48
+ $out = @fopen( "{$filePath}.part", $chunk == 0 ? 'wb' : 'ab' );
49
+ if ( $out ) {
50
+
51
+ /** Read binary input stream and append it to temp file. */
52
+ $in = @fopen( $_FILES['async-upload']['tmp_name'], 'rb' );
53
+
54
+ if ( $in ) {
55
+ while ( $buff = fread( $in, 4096 ) ) {
56
+ fwrite( $out, $buff );
57
+ }
58
+ } else {
59
+ /** Failed to open input stream. */
60
+ /** Attempt to clean up unfinished output. */
61
+ @fclose( $out );
62
+ @unlink( "{$filePath}.part" );
63
+ die();
64
+ }
65
+
66
+ @fclose( $in );
67
+ @fclose( $out );
68
+
69
+ @unlink( $_FILES['async-upload']['tmp_name'] );
70
+
71
+ } else {
72
+ /** Failed to open output stream. */
73
+ die();
74
+ }
75
+
76
+ /** Check if file has finished uploading all parts. */
77
+ if ( ! $chunks || $chunk == $chunks - 1 ) {
78
+
79
+ /** Recreate upload in $_FILES global and pass off to WordPress. */
80
+ rename( "{$filePath}.part", $_FILES['async-upload']['tmp_name'] );
81
+ $_FILES['async-upload']['name'] = $fileName;
82
+ $_FILES['async-upload']['size'] = filesize( $_FILES['async-upload']['tmp_name'] );
83
+ $_FILES['async-upload']['type'] = mime_content_type( $_FILES['async-upload']['tmp_name'] );
84
+ /** Set $_SERVER'[PHP_SELF'] global to wp-admin upload AJAX to stop a PHP notice from /wp-includes/vars.php line 31. */
85
+ $_SERVER['PHP_SELF'] = '/wp-admin/async-upload.php';
86
+ require_once( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . '/wp-admin/async-upload.php' );
87
+
88
+ }
tuxedo_big_file_uploads.php ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Plugin Name: Tuxedo Big File Uploads
4
+ * Plugin URI: https://github.com/andtrev/Tuxedo-Big-File-Uploads
5
+ * Description: Enables large file uploads in the built-in WordPress media uploader.
6
+ * Version: 1.0
7
+ * Author: Trevor Anderson
8
+ * Author URI: https://github.com/andtrev
9
+ * License: GPLv2 or later
10
+ * Domain Path: /languages
11
+ * Text Domain: tuxedo-big-file-uploads
12
+ *
13
+ * This program is free software; you can redistribute it and/or
14
+ * modify it under the terms of the GNU General Public License
15
+ * as published by the Free Software Foundation; either version 2
16
+ * of the License, or (at your option) any later version.
17
+ *
18
+ * This program is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License
24
+ * along with this program; if not, write to the Free Software
25
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26
+ *
27
+ * @package TuxedoBigFileUploads
28
+ * @version 1.0.0
29
+ */
30
+
31
+ /**
32
+ * Tuxedo Big File Uploads manager class.
33
+ *
34
+ * Bootstraps the plugin by hooking into plupload defaults and
35
+ * media settings.
36
+ *
37
+ * @since 1.0.0
38
+ */
39
+ class TuxedoBigFileUploads {
40
+
41
+ /**
42
+ * TuxedoBigFileUploads instance.
43
+ *
44
+ * @since 1.0.0
45
+ * @access private
46
+ * @static
47
+ * @var TuxedoBigFileUploads
48
+ */
49
+ private static $instance = false;
50
+
51
+ /**
52
+ * Get the instance.
53
+ *
54
+ * Returns the current instance, creates one if it
55
+ * doesn't exist. Ensures only one instance of
56
+ * TuxedoBigFileUploads is loaded or can be loaded.
57
+ *
58
+ * @since 1.0.0
59
+ * @static
60
+ *
61
+ * @return TuxedoBigFileUploads
62
+ */
63
+ public static function get_instance() {
64
+
65
+ if ( ! self::$instance ) {
66
+ self::$instance = new self();
67
+ }
68
+ return self::$instance;
69
+
70
+ }
71
+
72
+ /**
73
+ * Constructor.
74
+ *
75
+ * Initializes and adds functions to filter and action hooks.
76
+ *
77
+ * @since 1.0.0
78
+ */
79
+ public function __construct() {
80
+
81
+ add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
82
+ add_filter( 'plupload_init', array( $this, 'filter_plupload_settings' ) );
83
+ add_filter( 'plupload_default_settings', array( $this, 'filter_plupload_settings' ) );
84
+ add_filter( 'upload_size_limit', array( $this, 'filter_upload_size_limit' ) );
85
+ add_action( 'admin_init', array( $this, 'settings_api_init' ) );
86
+
87
+ }
88
+
89
+ /**
90
+ * Filter plupload settings.
91
+ *
92
+ * @since 1.0.0
93
+ */
94
+ public function filter_plupload_settings( $plupload_settings ) {
95
+
96
+ $tuxbfu_chunk_size = intval( get_option( 'tuxbfu_chunk_size', 512 ) );
97
+ if ( $tuxbfu_chunk_size < 1 ) {
98
+ $tuxbfu_chunk_size = 512;
99
+ }
100
+ $tuxbfu_max_retries = intval( get_option( 'tuxbfu_max_retries', 5 ) );
101
+ if ( $tuxbfu_max_retries < 1 ) {
102
+ $tuxbfu_max_retries = 5;
103
+ }
104
+ $plupload_settings['url'] = plugins_url( 'tux_handle_upload.php', __FILE__ );
105
+ $plupload_settings['filters']['max_file_size'] = $this->filter_upload_size_limit('') . 'b';
106
+ $plupload_settings['chunk_size'] = $tuxbfu_chunk_size . 'kb';
107
+ $plupload_settings['max_retries'] = $tuxbfu_max_retries;
108
+ return $plupload_settings;
109
+
110
+ }
111
+
112
+ /**
113
+ * Load Localisation files.
114
+ *
115
+ * @since 1.0.0
116
+ */
117
+ public function load_textdomain() {
118
+
119
+ $domain = 'tuxedo-big-file-uploads';
120
+ $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
121
+ load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' );
122
+ load_plugin_textdomain( $domain, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
123
+
124
+ }
125
+
126
+ /**
127
+ * Return max upload size.
128
+ *
129
+ * Free space of temp directory.
130
+ *
131
+ * @since 1.0.0
132
+ *
133
+ * @return float $bytes Free disk space in bytes.
134
+ */
135
+ public function filter_upload_size_limit( $unused ) {
136
+
137
+ $bytes = disk_free_space( sys_get_temp_dir() );
138
+ if ( $bytes === false ) {
139
+ $bytes = 0;
140
+ }
141
+ return $bytes;
142
+
143
+ }
144
+
145
+ /**
146
+ * Initialize settings api.
147
+ *
148
+ * Registers settings and setting fields.
149
+ *
150
+ * @since 1.0.0
151
+ */
152
+ public function settings_api_init() {
153
+
154
+ add_settings_field(
155
+ 'tuxbfu_chunk_size',
156
+ __( 'Chunk Size (kb)', 'tuxedo-big-file-uploads' ),
157
+ array( $this, 'settings_chunk_size_callback' ),
158
+ 'media',
159
+ 'uploads'
160
+ );
161
+ add_settings_field(
162
+ 'tuxbfu_max_retries',
163
+ __( 'Max Retries', 'tuxedo-big-file-uploads' ),
164
+ array( $this, 'settings_max_retries_callback' ),
165
+ 'media',
166
+ 'uploads'
167
+ );
168
+ register_setting( 'media', 'tuxbfu_chunk_size', 'intval' );
169
+ register_setting( 'media', 'tuxbfu_max_retries', 'intval' );
170
+
171
+ }
172
+
173
+ /**
174
+ * Output chunk size input control.
175
+ *
176
+ * @since 1.0.0
177
+ */
178
+ public function settings_chunk_size_callback() {
179
+
180
+ $tuxbfu_chunk_size = intval( get_option( 'tuxbfu_chunk_size', 512 ) );
181
+ if ( $tuxbfu_chunk_size < 1 ) {
182
+ $tuxbfu_chunk_size = 512;
183
+ }
184
+ $tuxbfu_chunk_size = esc_attr( $tuxbfu_chunk_size );
185
+ echo "<input type='text' name='tuxbfu_chunk_size' value='{$tuxbfu_chunk_size}' />";
186
+
187
+ }
188
+
189
+ /**
190
+ * Output max retries input control.
191
+ *
192
+ * @since 1.0.0
193
+ */
194
+ public function settings_max_retries_callback() {
195
+
196
+ $tuxbfu_max_retries = intval( get_option( 'tuxbfu_max_retries', 5 ) );
197
+ if ( $tuxbfu_max_retries < 1 ) {
198
+ $tuxbfu_max_retries = 5;
199
+ }
200
+ $tuxbfu_max_retries = esc_attr( $tuxbfu_max_retries );
201
+ echo "<input type='text' name='tuxbfu_max_retries' value='{$tuxbfu_max_retries}' />";
202
+
203
+ }
204
+
205
+ }
206
+
207
+ /** Instantiate the plugin class. */
208
+ $tux_big_file_uploads = TuxedoBigFileUploads::get_instance();
uninstall.php ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * TuxedoBigFileUploads Uninstall
4
+ *
5
+ * Uninstalling TuxedoBigFileUploads deletes all options.
6
+ *
7
+ * @package TuxedoBigFileUploads
8
+ * @since 1.0.0
9
+ */
10
+
11
+ /** Check if we are uninstalling. */
12
+ if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
13
+ exit;
14
+ }
15
+
16
+ /** Delete options. */
17
+ delete_option( 'tuxbfu_chunk_size' );
18
+ delete_option( 'tuxbfu_max_retries' );