Uploading an image on the server is a common task for developers. One may have done this task hundreds of times. During this upload process, developers have to take care of the following things.
- Check if the uploaded file is an image
- Create a directory where images will store
- Generate a unique name for each image so that it will not replace the existing one with the same name.
- Optionally add validation for the dimension of an image. If an uploaded image has a large dimension than specified then throw an error.
These are the recommended checks a developer should follow while writing code. One can write conditions for all these steps in the code. But it will take some work and the code becomes lengthy.
I will recommend using Bulletproof library which handles all these steps for you under the hood. It saves you the time of adding logic for each step and reducing the number of lines from your code.
Bulletproof is a secure image uploader, with a nice API. We have tried this library for our project and we are quite happy with the end result. That’s why I decided to share a word about this library with our readers.
The Bulletproof library has the following features in its core which makes it more secure.
- Uses exif_imagetype() to get the true image mime (.extension)
- Uses getimagesize() to check if the image has a valid height/width in pixels.
- Sanitized image names, strict folder permissions, and more…
That being said, let’s take a look at how to upload images in PHP using the Bulletproof library.
Getting Started
To get started with the Bulletproof library, install it using the Composer command below. Make sure you have Composer installed on the system.
composer require samayo/bulletproof:4.0.*
Next, create a simple HTML form with file input and submit button.
<form method="POST" enctype="multipart/form-data">
<p><input type="file" name="image" accept="image/*" required /></p>
<input type="submit" name="submit" value="Upload" />
</form>
Upload Image using Bulletproof Library
We are set with a library and HTML form. Now, let’s add a code provided by the library which uploads an image on the server.
<?php
require_once 'vendor/autoload.php';
if (isset($_POST['submit'])) {
$file = new Bulletproof\Image($_FILES);
$file->setLocation('uploads');
if ($file["image"]) {
$upload = $file->upload();
if ($upload) {
echo $upload->getFullPath();
} else {
echo $file->getError();
}
}
}
?>
You are done. Go ahead and test it. You will see your images stored inside the ‘uploads’ folder with a unique name. The user can change this location with something else.
If you try to upload files other than images then you will get an error. Just in case, if you want to set dimension also then add the below statement in the above code.
$file->setDimension(600, 400);
Here ‘600’ is the image width and ‘400’ is the height of the image. Adjust values as per your requirements.
Upload Multiple Images Using Bulletproof Library
You may want to upload multiple images on a server using this library. It requires a little bit of change in the above code. You need to add a ‘multiple’ attribute to the file input. Also, use the array format for the name of file input. After this, we will loop through each image and upload it to the server as follows.
<?php
require_once 'vendor/autoload.php';
if (isset($_POST['submit'])) {
for($i = 0; $i < count($_FILES['image']['name']); $i++) {
$arr_file = array(
"name" => $_FILES['image']['name'][$i],
"type" => $_FILES['image']['type'][$i],
"tmp_name" => $_FILES['image']['tmp_name'][$i],
"error" => $_FILES['image']['error'][$i],
"size" => $_FILES['image']['size'][$i],
);
$file = new Bulletproof\Image($arr_file);
$file->setLocation('uploads');
$upload = $file->upload();
}
}
?>
<form method="POST" enctype="multipart/form-data">
<p><input type="file" name="image[]" accept="image/*" multiple required /></p>
<input type="submit" name="submit" value="Upload" />
</form>
That’s it! I hope you understand how to upload an image through the Bulletproof library. Please share your thoughts and suggestions in the comment section below.
Related Articles
- A Guide to Upload and Compress Images in Laravel
- How to Optimize Image on Upload in PHP
- Resize Image in PHP using TinyPNG
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
But what if we have to upload multiple image files in the same form ?
I updated the article and explained how to deal with multiple images.
Thank you for your tutorial – I found your video on YouTube, but your instructions here are very clear!