• I’m not so good with PHP, so can you please have a look at my code and tell me if you can see any glaring holes ??

    This is rewritten wp_upload_dir() function from functions.php in WordPress 2.2

    The goals of rewrite were:
    1. change upload folder structure from YEAR/MONTH to YEAR/MONTH/DAY
    2. if current post is already saved, then use its timestamp for upload folder (so say if you are writing post set in 1999 – the upload will go to the correct folder of 1999/10/01 instead of current day 2007/05/27)
    3. enabled uploads outside of WordPress folder

    function wp_upload_dir() {
    	$xtra = "";
    
    	if (get_option('uploads_use_yearmonth_folders'))
    	{
    		global $post_id;	
    
    		$time = current_time('timestamp');
    
    		if (isset($post_id))
    		{
    			$mypost = get_post($post_id);
    
    			if (is_object($mypost))
    				$time = strtotime($mypost->post_date);
    		}
    
    		$y = date("Y", $time);
    		$m = date("m", $time);
    		$d = date("d", $time);
    
    		$xtra = "/$y/$m/$d";
    	}
    
    	$path = get_settings('upload_path');
    
    	$dir = FX_UPLOADPATH . $path . $xtra;
    	$url = FX_UPLOADURL . $path . $xtra;
    
    	if ( ! wp_mkdir_p( $dir ) ) {
    		$message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $dir);
    		return array('error' => $message);
    	}
    
    	$uploads = array('path' => $dir, 'url' => $url, 'error' => false);
    	return apply_filters('upload_dir', $uploads);
    }

    Special notes:
    FX_UPLOADPATH (filesystem path to uploads folder) and FX_UPLOADURL (web accessable URL to uploads folder) are defined in wp-config.php

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter futurix

    (@futurix)

    Updated code a little (now it checks if post is actually present).

    Hi Futurix. I needed something like this to organize the filesystem a little better when doing batch uploads. I took some of your code and made a proper plugin of it. Thanks for the idea. ??

    Note that the plugin does not implement your third goal – “enable uploads outside of WordPress folder”. I’m using whatever directory the user defined in WP options instead of having them manually define FX_UPLOADPATH and FX_UPLOADURL in the config like you did.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Criticize my hack (different upload folder)’ is closed to new replies.