Make WordPress Core

Changeset 3389


Ignore:
Timestamp:
01/02/2006 04:33:27 AM (20 years ago)
Author:
ryan
Message:

Upload dir cleanups. Honor old fileupload_path settings if present.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions-post.php

    r3388 r3389  
    852852}
    853853
     854function wp_mkdir_p($target) {
     855    // from php.net/mkdir user contributed notes
     856    if (file_exists($target)) {
     857        if (! @ is_dir($target))
     858            return false;
     859        else
     860            return true;
     861    }
     862
     863    // Attempting to create the directory may clutter up our display.
     864    if (@ mkdir($target)) {
     865        $stat = @ stat(dirname($target));
     866        $dir_perms = $stat['mode'] & 0007777;  // Get the permission bits.
     867        @ chmod($target, $dir_perms);
     868        return true;
     869    } else {
     870        if ( is_dir(dirname($target)) )
     871            return false;   
     872    }
     873
     874    // If the above failed, attempt to create the parent node, then try again.
     875    if (wp_mkdir_p(dirname($target)))
     876        return wp_mkdir_p($target);
     877
     878    return false;
     879}
     880
    854881// Returns an array containing the current upload directory's path and url, or an error message.
    855882function wp_upload_dir() {
    856         if ( defined('UPLOADS') )
    857                 $dir = UPLOADS;
    858         else
    859                 $dir = 'wp-content/uploads';
    860 
    861     $path = ABSPATH . $dir;
    862    
    863     // Give the new dirs the same perms as wp-content.
    864     $stat = stat(ABSPATH . 'wp-content');
    865     $dir_perms = $stat['mode'] & 0007777;  // Get the permission bits.
    866 
    867         // Make sure we have an uploads dir
    868         if ( ! file_exists( $path ) ) {
    869                 if ( ! @ mkdir( $path ) )
    870                         return array('error' => "Unable to create directory $path. Is its parent directory writable by the server?");
    871         @ chmod( $path, $dir_perms );
    872     }
    873 
    874         // Generate the yearly and monthly dirs
    875         $time = current_time( 'mysql' );
    876         $y = substr( $time, 0, 4 );
    877         $m = substr( $time, 5, 2 );
    878         $pathy = "$path/$y";
    879         $pathym = "$path/$y/$m";
    880 
    881         // Make sure we have a yearly dir
    882         if ( ! file_exists( $pathy ) ) {
    883                 if ( ! @ mkdir( $pathy ) )
    884                         return array('error' => "Unable to create directory $pathy. Is $path writable?");
    885         @ chmod( $pathy, $dir_perms );
    886     }
    887 
    888         // Make sure we have a monthly dir
    889         if ( ! file_exists( $pathym ) ) {
    890                 if ( ! @ mkdir( $pathym ) )
    891                         return array('error' => "Unable to create directory $pathym. Is $pathy writable?");
    892         @ chmod( $pathym, $dir_perms );
    893     }
    894 
    895     $uploads = array('path' => $pathym, 'url' => get_option('siteurl') . "/$dir/$y/$m", 'error' => false);
     883    $dir = trim(get_settings('fileupload_realpath'));
     884    $url = trim(get_settings('fileupload_url'));
     885
     886    $custom = true;
     887    if ( empty($dir) || empty($url) ) {
     888        $dir = ABSPATH . 'wp-content/uploads';
     889        $url = get_option('siteurl') . '/wp-content/uploads';
     890        $custom = false;
     891    }
     892
     893    if ( defined('UPLOADS') ) {
     894        $dir = ABSPATH . UPLOADS;
     895        $url =  get_option('siteurl') . '/' . UPLOADS;
     896        $custom = false;
     897    }
     898
     899    if ( ! $custom) {
     900        // Generate the yearly and monthly dirs
     901        $time = current_time( 'mysql' );
     902        $y = substr( $time, 0, 4 );
     903        $m = substr( $time, 5, 2 );
     904        $dir = $dir . "/$y/$m";
     905        $url = $url . "/$y/$m";
     906    }
     907
     908    // Make sure we have an uploads dir
     909    if ( ! wp_mkdir_p( $dir ) ) {
     910        $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $dir);
     911        return array('error' => $message);
     912    }
     913
     914    $uploads = array('path' => $dir, 'url' => $url, 'error' => false);
    896915    return apply_filters('upload_dir', $uploads);
    897916}
Note: See TracChangeset for help on using the changeset viewer.