Information



Tags



Share

How to get properties of uploaded images in PHP

Advertisement:


read_later

Read Later

Keywords



Keywords



read_later

Read Later

Information

Tags

Share





Advertisement

Advertisement



Definition

When you create a user interface information form, you might need a profile picture to be saved on the database for further usage. In this article, I will show you how to get properties of uploaded images – name, temporary name, size, width, height, and extension – in PHP.

Code

If the form is submitted, get the uploaded file properties.

If the name of the uploaded file is not empty, save properties in an associative array named img.

To get the width and height of the uploaded file(image), use the @getimagesize method as depicted on the code below.

To get the extension of the uploaded file(image), use this line – pathinfo($image["name"], PATHINFO_EXTENSION).


<?php
if($_FILES['image'])){ $image = $_FILES['image']); }

if(!empty($image['name'])){
$img = array(
"name" => $image["name"],
"tmp_name" => $image["tmp_name"],
"size" => $image["size"],
"width" => @getimagesize($image["tmp_name"])[0],
"height" => @getimagesize($image["tmp_name"])[1],
"extension" => pathinfo($image["name"], PATHINFO_EXTENSION)
);
?>

<input type="file" name="image" />