Uploading files such as Images, videos and other text documents may easily be uploaded using $_FILES default array in PHP 5.0. If you want to just test uploading files in PHP & MySQL then I’m here to help you out regarding this via video tutorials. File is a kind of media which has different parts to be uploaded to the files’ server. I’ll soon create one more tutorial in which you’ll be learning how to upload the files to the MySQL database and then re-call wherever you need them in the pages.
You can upload any kind of media using PHP, but this tutorial is just the basic idea for uploading a single file to file server but not to the MySQL database. However, you may upload different files at the same time using the same method.
Before going to learn this, you may want to also know about:
- How to create a registration form in PHP?
- How to create a search engine in PHP?
- Complete CMS & website in PHP
- PHP A to Z video tutorials
What we’ll need to do?
- Create a file with the name “upload_image.php“
- Save the file and give the name & extension as “upload_image.php“
- Now copy and paste all below source code into that file and save that
<form action=”upload_image.php” method=”post” enctype=”multipart/form-data”>
Select Image:<input type=”file” name=”image”>
<input type=”submit” name=”upload” value=”Upload Now”>
$image_name = $_FILES[‘image’][‘name’];
$image_type = $_FILES[‘image’][‘type’];
$image_size = $_FILES[‘image’][‘size’];
echo “<script>alert(‘Please Select an Image’)</script>”;
echo “Image Uploaded Successfully Here is the image”;
echo “<img src=’photos/$image_name’>”;
In the above source code, everything looks just fine and simple except the “enctype=”multipart/form-data” which is always added as an attribute to the <form> tag when dealing with media files. Without adding enctype= you can’t add files into the server..
The $_FILES array is always used to get the media files, and there are three information which are stored in the local variables, because the media files are not a single data file, they might have different data types.
The Following three parameters are necessary to be saved into local variables:
- $image_name = $_FILES[‘image’][‘name’];
- $image_type = $_FILES[‘image’][‘type’];
- $image_size = $_FILES[‘image’][‘size’];
The name of the image is always saved into a local variable for using it later on, in the website, while the type of the image is used to set a file validation for the file. Because if we don’t set that then anyone can upload any type of file. And the “Size” is used to store the size of the image/file into a local variable, so we can then limit to a fixed size if we want.
The move_uploaded_file is a default function in PHP which moves the file into the files server folder which we mention in the line as you can see in the above source code.
This is very simple tutorial for uploading images using PHP, if you still need to know practically then you are welcome to watch below video tutorials for learning it more closely:
Uploading files to the server tutorial video #1
Uploading files to the server tutorial video #2
After watching the videos, I can just assume that you’ve learned what I tried to teach you in these videos 🙂 for more videos you can connect with this blog either via email or via Facebook.
Moreover, take few seconds to share this tutorial with all of your friends on social media and have a great time with PHP 😛