Digitalraindrops

Desination Directory

Feb 6th, 2011
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. <?php
  2. // Website: http://www.digitalraindrops.net
  3.  
  4. // I am working on a WordPress templating plugin and needed to upload new files to a specific folder in the uploads directory, without changing the default settings, I borrowed some code from wp-download
  5. // This code may help others, first this function re-directs the uploads to '/wp-content/uploads/mydirectory/images/'
  6.  
  7. // First Example: Changed the Destination Directory for Uploads
  8. $sub_dir='/my-directory/images';
  9.  
  10. function drfb_upload_dir($upload) {
  11.     global $sub_dir;
  12.     $upload['path'] = $upload['basedir'] .$sub_dir;
  13.     $upload['url'] = $upload['baseurl'] .$sub_dir;
  14.     return $upload;
  15. }
  16.  
  17. // You call it before you upload, this sets it directory, you need to refresh or remove the filter after upload.
  18. add_filter('upload_dir', 'drfb_upload_dir');
  19.  
  20. // Do the upload and the image will be uploaded to '/wp-content/uploads/mydirectory/images/'
  21.  
  22. if( has_filter( 'upload_dir', 'drfb_upload_dir' ) )remove_filter( 'upload_dir', 'drfb_upload_dir' );
  23.  
  24.  
  25. // Second Example: this will take a returned libary image url, and copy it to the new directory.
  26. $dst= $upload['basedir'] .'/my-directory/images/';
  27.  
  28. //Lets say the looked up image url is stored in an option.
  29. $myimage = get_option('my-image');
  30. if ( $myimage ) {
  31.     $file_name = basename( $myimage );
  32.     $file = WP_CONTENT_DIR .$file_name;
  33.     if ( file_exists( $file ) && !file_exists( $dst .$file_name )) {
  34.         if ( @copy( $file , $dst .$file_name ) ) update_option('my-image','');
  35.     }
  36. }
Add Comment
Please, Sign In to add comment