Welcome Guest, Not a member yet? Register   Sign In
How to get file info before uploading
#1

[eluser]EyeAmN8[/eluser]
I am making a site that manages multiple file types. The end user will have a form that has the options to select a project, a file(s) to upload, and enter a description about it. I was wondering if there is anyway to use CI's built in goodness and check the file type ext before uploading it? Reason I want to do that, is because I want to separate files into different folders ex: documents, images, sound...
So, my main thing I want to do is to assign upload paths depending on the file type that is being uploaded.
Any ideas???

Thanks!
#2

[eluser]EyeAmN8[/eluser]
Also, I want to point out that I am trying to avoid doing anything that would involve uploading to a temp dir, then moving to the correct dir after the upload. I know that there a lot of server side options after the upload has taken place like calling a shell script to move the file, or using php to do it...
#3

[eluser]boltsabre[/eluser]
>> I am trying to avoid doing anything that would involve uploading to a temp dir, then moving to the correct dir after the upload.
That's how it operates and you cannot get around it. When user selects a file and submits a form, the file gets loaded into a temp directory. It's then up to you to move it to your own folder if you want to keep the file. This design cannot be circumvented, it's like this for various security reasons (for one example, see my last paragraph).

>>I was wondering if there is anyway to use CI’s built in goodness and check the file type ext before uploading it?
Well, a quick google search would have saved you waiting on this reply. Use the force Luke, embrace google!!!

Search pharse: php file upload get extension
Search result: https://www.google.com/search?q=php+file...+extension
Surprise surprise, the first two results give good clear examples of how to get the file extension using php!

Search pharse: codeigniter file upload get extension
Search result: https://www.google.com/search?q=codeigni...+extension
Surprise surprise, the first two results link to various forum topics about this and... wait for it... the official codeigniter user guide page on the file upload library!!! Which by the way does have a method to get the file extension. Check down the bottom of the page.

On a side note, you may want to research this a little deeper... there is what is known as the "double extension" hack. Basically I upload a file called "my_hacker_file.php.jpg". It is in reality a .php file (containing my evil hacker code) but is generally treated as a .jpg file because of the double extension. You'll have to have a look, experiment, test your script to make sure it is not susceptible to this type of hack (easy done, create a .php file, save it, add .jpg onto the end of the file name, upload it. It it uploads your in trouble). See... if you'd gotten around the "temp directory" thing (which you can't so don't waste your time trying) and not checked for double extensions, someone could have uploaded whatever they wanted direct onto a public directory on your server... not good!

#4

[eluser]EyeAmN8[/eluser]
boltsabre,
Thanks for your reply! I guess I was looking in the wrong direction for this one. Also, thanks for the tip about the double ext type.
I will try this given info out and post back with my result
#5

[eluser]EyeAmN8[/eluser]
I got it!
I ended up using:
Code:
$file = $_FILES['userfile']['name'];
$ext = substr(strrchr($file, '.'), 1);
I found a lot of examples at http://cowburn.info/2008/01/13/get-file-extension-comparison/
I could probably condense it into one line, but it works
Thanks




Theme © iAndrew 2016 - Forum software by © MyBB