![]() |
Uploading Files with PHP
Uploading Files with PHP
With the release of PHP 4.1.0 came an easier and more secure method of uploading files. In this tutorial, we’ll be looking at the superglobal $_FILES, which allows us to create a basic PHP uploader in only a few lines of code. Because it is a superglobal, it is available in all scopes throughout the script. Our first step in coding an upload script is creating the HTML form. This form will allow a user to browse for a file and then upload it to a directory. This is the code we will be using: PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
|
Nice introduction to file uploads. Thanks for contributing daz :)
|
Just to add onto this article. There is a function in PHP that allows you to check if the file is actually uploaded, and that it's not coming from anywhere else:
is_uploaded_file(). I would also be tempted to check both the extension and the MIME type for extra security. Perhaps that's just me being somewhat cautious though.Thanks for the article. It's very much appreciated and I'm sure it will give many people a helping hand! |
Cool! Nice tutorial. :)
IE is kinda funny with how it gives it's mime types. For example it sends image/pjpeg where firefox or other browsers would just use image/jpeg. You've already got it in your list, but there is another one that IE uses for PNG, so you might want to expand the allowed types to: PHP Code:
Though I wouldn't stop using this method as its definitely a good test, it should in fact be built upon using the other method you and Wildhoney suggested where the actual file extension is checked. If we do this, we can ensure that no file that is uploaded has a .php/.pl/etc extension and thus can't be executed. To do this, we'd use a white list again that is a a list of allowed extensions: PHP Code:
PHP Code:
PHP Code:
|
| All times are GMT. The time now is 03:13 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0