Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I was able to temporarily disable an automatic task of wordpress during a specific file type upload which is nice. For instance, I changed the usual upload path for the pdf uploads by doing this:

//change the normal path
update_option('upload_path','wp-content/uploads/attachments/'.$id);
//upload the file
$upload = wp_upload_bits($pdffile,null,file_get_contents($pdffile_temp));
//get the standard path back
update_option('upload_path','');

Now I would like to do the same with the "wp unique filename" function that is automatically called by wordpress. The same way I changed temporarily the path for this specific upload routine, I would like not to rename the file if another one has the same name inside the target folder (I would rather overwrite the old file with the new one instead).

How could I do that?

Thank you.

share|improve this question
add comment

1 Answer

Welcome to the lions den

So you're willing to get down into the blazing furnace or the lions den and change the upload path. This is so not a good idea without investigating what is happening behind the scenes. I can't give you a full write up, as there's so much involved, like filters, options calls, constants, etc. but I can give you one recommendation: Don't. Do. This. Things like that tend to work for a while. And then suddenly break, as you hit one of the edge cases. And you'll be left wondering what happened. Callbacks, options, etc. will hide the problem from you and you will spend ages to trace this bug down.

EDIT

Don't hand/hard code such stuff.

// Get original value
$temp = get_option( 'upload_path' );
// Assign custom value
update_option( 'upload_path', "wp-content/uploads/attachments/{$id}" );

// do custom stuff

// Move old value back in
update_option( 'upload_path', $temp );

The wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) function takes three arguments. The third is a custom callback and optional. It takes three arguments: $dir, $name, $ext. Just use this one if you need to do this custom.

  • $dir - The first argument from the wp_unique_filename() fn
  • $ext - retrieved via pathinfo() (PHP default fn) - the file extension. In your case if ( 'pdf' === strtolower( $ext ) ).
  • $name - The filename without extension

The custom callback can also be completely disabled (I don't know what then happens - maybe the sky falls on your head?). You can do this by simply using the core functions of __return_false, __return_null (or maybe even __return_zero?).

share|improve this answer
    
I created a plugin which is completely independant from the rest of wordpress. There is no intrication and when I change something, as you can see above, i directly put it back to its previous state as soon as the task is completed. Regarding the path: my upload system is related to a custom upload folder and the plugin hooks to a brand new custom table of the database. Not to mention that I make the querys (in and out) from custom php functions. There is no mess with the system here and I would like wordpress not to mess with duplicating filenames that are not supposed to be managed by it. –  Baylock Nov 3 '12 at 16:57
1  
Besides, the blog is meant to live 24 hours only, it's not something which could cause some side effects in the long term. The needs and use cases are very limited and very well known but also very specific. This file upload system is a personal creation as no wordpress plugin known so far can do the job as demanded. The plugin is almost done and works well. All I need now to finish this plugin is to know how to overwrite old files instead of renaming the new ones. Any technical help would be appreciated. –  Baylock Nov 3 '12 at 17:10
    
@Baylock Fully understand if you know what you're doing. See update. –  kaiser Nov 3 '12 at 17:39
    
Thank you but, if I'm right, you're showing me how to use it. What I would like is to bypass it as it's triggered by default and I don't want to. Did I miss something? Probably. –  Baylock Nov 3 '12 at 23:36
1  
Bypass it with a custom callback. That can even be __return_false or __return_null. I don't know what the default callback is returning. You'll have to try that one out yourself. –  kaiser Nov 4 '12 at 14:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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