Welcome Guest, Not a member yet? Register   Sign In
File Upload Class: Pass in new file name

[eluser]louis w[/eluser]
It would be nice to be able to pass a value in to the do_upload method which is what you want the uploaded file to be named. I am saving files by their id, so need to rename it.

I can hack this by changing the name in $_FILES, but a real solution would be better.

[eluser]Chris Newton[/eluser]
Upon upload, you could rename the file, using a native PHP method.

rename($file_data['file_name'], 'new_name'.$file_data['file_ext']);

The upload class can rename a file to keep from overwriting an existing file anyway, so there's no fear of tacking this on, post-upload. Doing the above way is quick and easy, but might be more computationally expensive than having a config option for the upload class that does the same thing.

I've just come across the same problem, luckily I had already created a "MY_Upload" class and I simply copied over the "do_upload" method and added some code to change the filename like this:

// if a new file name was set
if($new_file_name != null) {
    $this->file_name = $new_file_name;

This was inserted before the method validates the filename for an existing file with the same name. Works like a charm.

Theme © iAndrew 2016 - Forum software by © MyBB