-
- // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
- $upload_error_strings = array( false,
- __( "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ),
- __( "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ),
- __( "The uploaded file was only partially uploaded." ),
- __( "No file was uploaded." ),
- '',
- __( "Missing a temporary folder." ),
- __( "Failed to write file to disk." ),
- __( "File upload stopped by extension." ));
-
- // All tests are on by default. Most can be turned off by $overrides[{test_name}] = false;
- $test_form = true;
- $test_size = true;
-
- // If you override this, you must provide $ext and $type!!!!
- $test_type = true;
- $mimes = false;
-
- // Install user overrides. Did we mention that this voids your warranty?
- if ( is_array( $overrides ) )
- extract( $overrides, EXTR_OVERWRITE );
-
- // A correct form post will pass this test.
- if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
- return $upload_error_handler( $file, __( 'Invalid form submission.' ));
-
- // A successful upload will pass this test. It makes no sense to override this one.
- if ( ! empty( $file['error'] ) )
- return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
-
- // A non-empty file will pass this test.
- if ( $test_size && !(filesize($file['tmp_name']) > 0 ) )
- return $upload_error_handler( $file, __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini.' ));
-
- // A properly uploaded file will pass this test. There should be no reason to override this one.
- if (! @ is_file( $file['tmp_name'] ) )
- return $upload_error_handler( $file, __( 'Specified file does not exist.' ));
-
- // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
- if ( $test_type ) {
- $wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes );
-
- extract( $wp_filetype );
-
- // Check to see if wp_check_filetype_and_ext() determined the filename was incorrect
- if ( $proper_filename )
- $file['name'] = $proper_filename;
-
- if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
- return $upload_error_handler( $file, __( 'Sorry, this file type is not permitted for security reasons.' ));
-
- if ( !$ext )
- $ext = ltrim(strrchr($file['name'], '.'), '.');
-
- if ( !$type )
- $type = $file['type'];