Uploading files/images is a task that is required in almost all web applications.
While uploading file is a very simple task, still lot of beginners in PHP get stuck in this step. So in this post I decided to share the functions I learned while I was a beginner, and I still continue to use them because of it’s simplicity and usefulness.
For all my file uploading needs I use the class.upload.php written by Colin Verot. Ok, I know all about PHP functions like move_uploaded_file() and is_uploaded_file(), still I prefer to use class.upload.php file because, this file provides me with good set of image manipulation functions along with a neat way to know when a file has been uploaded.
So, let’s get started on, how to easily upload a file in PHP.
First, let’s create a simple HTML form for uploading files.
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Upload file: <input type="file" name="fileupload">
<input type="submit" value="save">
</form>
It is important to note that the form’s enctype is multipart/form-data. This a place where novice developers make lot’s of mistake, they will forget to add enctype attribute to form and them spends hours thinking why they are not able to upload files or images.
Now, that we have a form to upload files, let’s see how we can handle file uploads on server.
I am going to cover four scenario’s here.
(more…)