1

i have been trying to upload the attachments to the custom directory without affecting the old uploads. so i use this bellow code to achieve that

add_filter('wp_handle_upload_prefilter', 'cust_pre_upload');
add_filter('wp_handle_upload', 'cust_post_upload');

function cust_pre_upload($file){
add_filter('upload_dir', 'cust_dir');
return $file;
}

function cust_post_upload($fileinfo){
remove_filter('upload_dir', 'cust_dir');
return $fileinfo;
}


function cust_dir( $args ) {
$customdir ="/mars";
$args['path'] = "C:/wamp/www/web1/wp-content";
$args['url']  = "http://localhost/web1/wp-content";

$args['subdir']  = $customdir;
$args['path']   .= $customdir; 
$args['url']    .= $customdir;     
return $args;
}

i use the wp_handle_upload_prefilter to assign the new directory only for the attachment i upload now or in future and wp_handle_upload to prevent the old attacmments. this code perfectly uploading to the folder where it supposed to [http://localhost/web1/wp-content/mars/file.jpg but the attachment get this wrong and show an invalid url of the attachment something like this [http://localhost/web1/wp-content/uploads/C:/wamp/www/web1/wp-content/mars/file.jpg] any help or idea would be appreciated :) tried different types of codes nothing worked out.

3
  • If you upload a file that has the same name, WordPress won't overwrite the old one. It will still create a new file each time you upload something. Oct 7, 2012 at 0:49
  • What happens if you use forward slashes / everywhere as you should? /mars, C:/wamp/www/web1/wp-content. Don’t worry about Windows, it will handle that without problems.
    – fuxia
    Oct 7, 2012 at 4:14
  • @toscho already tried with / but no luck still the same prob. its uploading correctly to the specified folder but shows the wrong attachment url as [localhost/web1/wp-content/uploads/C:/wamp/www/web1/wp-content/…
    – Felix
    Oct 7, 2012 at 18:24

1 Answer 1

0

If I remember right you need to chdir() into your custom uploads directory (before saving anything there) so that paths are correctly processed and saved in meta.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.