Welcome Guest, Not a member yet? Register   Sign In
Keep file extension while uploading an image
#1

[eluser]Unknown[/eluser]
Before the *update* of the upload library last month we could override the name of the file without specifying an extension keeping the original extension of the file.

Now after the update we are obliged to specify an extension in the name override or else "The file you're trying to upload is not valid"

Any thoughts?
#2

[eluser]mi6crazyheart[/eluser]
From u'r description i really couldn't able to understand what's actually u want to ask. But, if u want to know how to get extension of the file which u want to upload then you can use this piece of codes...
Code:
$path_info = pathinfo($_FILES["userfile"]["name"]); // "userfile" is the form input field name
$fileExtension = $path_info[‘extension’];
#3

[eluser]Unknown[/eluser]
[quote author="mi6crazyheart" date="1282340412"]From u'r description i really couldn't able to understand what's actually u want to ask. But, if u want to know how to get extension of the file which u want to upload then you can use this piece of codes...
Code:
$path_info = pathinfo($_FILES["userfile"]["name"]); // "userfile" is the form input field name
$fileExtension = $path_info[‘extension’];
[/quote]

Ok, I'll try to explain it better. There's a thread in this forum, the first one, called "CodeIgniter 1.7.2 Security Patch" which rewrites the upload.php library.

Before that patch, when uploading a file you needed to specify:

Code:
$config['allowed_types'] = 'gif|jpg|jpeg|png';

And you could specify

Code:
$config['file_name'] = 'some_name'

The last one let you override the original file name with 'some_name' while keeping the original extension of the file. E.g: the user uploads
Code:
a.gif
and it is saved in the server as
Code:
some_name.gif

Now, after the patch you must (at least I do in order for it to work) specify an extension in

Code:
$config['file_name'] = 'some_name.gif'

And that extension being in the

Code:
$config['allowed_types'] = 'gif|jpg|jpeg|png';

The problem with this is that it overrides the original file extension. E.g: user uploads
Code:
a.png
and it is saved in the server as
Code:
some_name.gif
which is wrong.

If you don't specify an extension in

Code:
$config['file_name'] = 'some_name'

after the patch it will raise an error telling you that the file you're trying to upload its not a valid file, as I assume there's no 'blank' extension in the allowed types.

Thing is, I want to keep the original extension while using the file_name override.
#4

[eluser]mi6crazyheart[/eluser]
Hey, the code which i've given u... will do the same thing. It'll help u bring the original extension of the file which u can use to build the original file name.

Like this:
Code:
$path_info = pathinfo($_FILES["userfile"]["name"]); // "userfile" is the form input field name
$fileExtension = $path_info[‘extension’];
$config[‘file_name’] = 'some_name.'.$fileExtension ;


In this way, u can upload u'r file with it's original extension...
#5

[eluser]franboud[/eluser]
Thank you mi6crazyheart!
It's exactly what I was looking for!




Theme © iAndrew 2016 - Forum software by © MyBB