Criticize my hack (different upload folder)
-
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 folderfunction 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
- The topic ‘Criticize my hack (different upload folder)’ is closed to new replies.