Example #1
0
function postie_handle_upload(&$file, $overrides = false, $time = null)
{
    // The default error handler.
    if (!function_exists('wp_handle_upload_error')) {
        function wp_handle_upload_error(&$file, $message)
        {
            return array('error' => $message);
        }
    }
    // You may define your own function and pass the name in $overrides['upload_error_handler']
    $upload_error_handler = 'wp_handle_upload_error';
    // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
    $action = 'wp_handle_upload';
    // 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."));
    // Install user overrides. Did we mention that this voids your warranty?
    if (is_array($overrides)) {
        extract($overrides, EXTR_OVERWRITE);
    }
    // A successful upload will pass this test. It makes no sense to override this one.
    if ($file['error'] > 0) {
        return $upload_error_handler($file, $upload_error_strings[$file['error']]);
    }
    // A non-empty file will pass this test.
    if (!($file['size'] > 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 (!file_exists($file['tmp_name'])) {
        return $upload_error_handler($file, __('Specified file failed upload test.'));
    }
    // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
    $wp_filetype = wp_check_filetype($file['name']);
    DebugEcho("postie_handle_upload: detected file type for " . $file['name'] . " is " . $wp_filetype['type']);
    extract($wp_filetype);
    if ((!$type || !$ext) && !current_user_can('unfiltered_upload')) {
        return $upload_error_handler($file, __('File type does not meet security guidelines. Try another.'));
    }
    if (!$ext) {
        $ext = ltrim(strrchr($file['name'], '.'), '.');
    }
    if (!$type) {
        $type = $file['type'];
    }
    // A writable uploads dir will pass this test. Again, there's no point overriding this one.
    if (!(($uploads = wp_upload_dir($time)) && false === $uploads['error'])) {
        return $upload_error_handler($file, $uploads['error']);
    }
    // fix filename (encode non-standard characters)
    $file['name'] = filename_fix($file['name']);
    $filename = wp_unique_filename($uploads['path'], $file['name']);
    DebugEcho("wp_unique_filename: {$filename}");
    // Move the file to the uploads dir
    $new_file = $uploads['path'] . "/{$filename}";
    if (false === @rename($file['tmp_name'], $new_file)) {
        DebugEcho("upload: rename failed");
        DebugEcho("new file: {$new_file}");
        //DebugDump($file);
        //DebugDump($uploads);
        return $upload_error_handler($file, sprintf(__('The uploaded file could not be moved to %s.'), $uploads['path']));
    }
    // Set correct file permissions
    $stat = stat(dirname($new_file));
    $perms = $stat['mode'] & 0666;
    @chmod($new_file, $perms);
    // Compute the URL
    $url = $uploads['url'] . "/{$filename}";
    $return = apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $type));
    return $return;
}
Example #2
0
 function test_filename_fix()
 {
     $f = "simple.png";
     $this->assertEquals($f, filename_fix($f));
     $f = "moiré-pättern.png";
     $this->assertEquals("moireCC81-paCC88ttern.png", filename_fix($f));
     $f = "וְאָהַבְתָּ.png";
     $this->assertEquals("D795D6B0D790D6B8D794D6B7D791D6B0D7AAD6BCD6B8.png", filename_fix($f));
     $this->assertEquals("D09AD0BED0B3D0B0D182D0BE.png", filename_fix("Когато.png"));
 }