Welcome Guest, Not a member yet? Register   Sign In
File Upload Class: Change file name
#1

[eluser]jedre[/eluser]
It's possible to upload file and change name using File Upload Class?

for example:

Before upload: my_file.png
After upload (the same file on server): 1.png

I wanna upload file and change name.
#2

[eluser]Gavin Vickery[/eluser]
Why do you want to rename the file? Personal preference, or in case another file with the same filename is uploaded?

If its because of duplicate filenames, the File Upload class already checks for this, and adds a number on to the file name (unless of course you allow overwrites in the File Upload config).

If you still want to change the filename, you could just rename it using PHP's native rename function after the file has been uploaded.

Example:
Code:
rename($data['file_name'], 'new_name'.$data['file_ext']);
If your file was 'example.jpg', this would change it to 'new_name.jpg'. If you need to use the path to the file name, you could do this:
Code:
rename($data['file_path'].$data['file_name'], $data['file_path'].'new_name'.$data['file_ext']);

Hope that answers your question.
#3

[eluser]marcoss[/eluser]
[quote author="Gavin Vickery" date="1194496934"]If its because of duplicate filenames, the File Upload class already checks for this, and adds a number on to the file name (unless of course you allow overwrites in the File Upload config).[/quote]

The point is that it should be in the Image Library, which currently is missing that basic option, I've already filled a feature request in the forums, but had no answer from the admins.
#4

[eluser]Gavin Vickery[/eluser]
But thats one line of code? How is that hard to implement? I could see if you had to write a bit of code, but common.
#5

[eluser]ELRafael[/eluser]
i solved a very closer problem with this

http://ellislab.com/forums/viewthread/61244/
#6

[eluser]Lionel H[/eluser]
Hello,

I use a solution that changes the filename before the upload, but as I'm not a PHP master, I'm not sure it's the perfect solution (but it works) :
Code:
$value = $_FILES['userfile']['name'];

$value = utf8_decode($value);

$value = strtr($value, utf8_decode('ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝÑñàáâãäåçèéêëìíîïðòóôõöùúûüýÿ'), 'AAAAAACEEEEIIIIOOOOOUUUUYNnaaaaaaceeeeiiiioooooouuuuyy');

$value = preg_replace('/( +)/i', '_', $value);

$value = strtolower($value);

$_FILES['userfile']['name'] = $value;
#7

[eluser]metamorpher[/eluser]
[quote author="Gavin Vickery" date="1194558959"]But thats one line of code? How is that hard to implement? I could see if you had to write a bit of code, but common.[/quote]

what's the point if you use for example the CI validator to prevent files renaming?

So, if I implement rename I should do the same work the CI preventor twice...

For example, in my language we use a lot of punctuation stuff like á é í ó ú, and some users might have their files named like that...

I tried the spaces remover thinking it would "normalize" the special chars but it don't, and wouldn't like to use a hack in the upload class to fix that...
#8

[eluser]xwero[/eluser]
metamorpher this thread is 2 years old, i don't think any one is losing sleep over this now.
#9

[eluser]metamorpher[/eluser]
sorry, didn't notice it, but I'm having the same trouble and I think this hack is my only solution for now

http://ellislab.com/forums/viewthread/61244/


'cause I couldn't find some topic explaining it well, and user guide wasn't helpful at all about this...
#10

[eluser]metamorpher[/eluser]
sorry, sorry... no more anoying metamorpher...

I realized that just can change the $_FILES[input]['name'] variable and that was it..... i'm so embarrased :$




Theme © iAndrew 2016 - Forum software by © MyBB