Get Image File Size Using PHP

This code will show you how to Get Image File Size using php. The program can upload image file and display the file size from the database server. You can use this code if you want to know the actual size of your uploaded file. To learn more about this tutorial, just follow the step below.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP https://www.apachefriends.org/index.html And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/

Creating Database

Open your database web server then create a database name in it db_get_size after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php
  1. <?php
  2.         $conn=mysqli_connect("localhost", "root", "", "db_get_size");
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

Creating the Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">Get Image File Size Using PHP</h3>
  16.                 <hr style="border-top:1px dottec #ccc;"/>
  17.                 <div class="col-md-8">
  18.                         <table class="table table-bordered">
  19.                                 <thead class="alert-info">
  20.                                         <tr>
  21.                                                 <th>Image Name</th>
  22.                                                 <th>Action</th>
  23.                                         </tr>
  24.                                 </thead>
  25.                                 <tbody>
  26.                                         <?php
  27.                                                 require'conn.php';
  28.                                                
  29.                                                 $query=mysqli_query($conn, "SELECT *FROM `image`") or die(mysqli_error());
  30.                                                 while($fetch=mysqli_fetch_array($query)){
  31.                                                         echo"<tr><td>".$fetch['image_name']."</td><td><a href='get_imagesize.php?file_name=".$fetch['image_name']."'>Get Image Size</a></td></tr>";
  32.                                                 }
  33.                                         ?>
  34.                                
  35.                                 </tbody>
  36.                         </table>
  37.                 </div>
  38.                 <div class="col-md-4">
  39.                         <form method="POST" action="upload.php" enctype="multipart/form-data">
  40.                                 <label style="font-size:18px;">Filename:</label>
  41.                                 <div class="form-group">
  42.                                         <input type="file" name="image" required="required"/>
  43.                                 </div>
  44.                                 <center><button class="btn btn-primary" name="upload">Upload</button></center>
  45.                         </form>
  46.                 </div>
  47.         </div>
  48. </body>
  49. </html>

Creating Upload Function

This code contains the upload function of the application. The code will send a PHP file request to store the image file to database server. To make this just copy and write these block of codes inside the text editor, then save it as upload.php
  1. <?php
  2.  
  3.         date_default_timezone_set("Etc/GMT+8");
  4.        
  5.         require_once 'conn.php';
  6.        
  7.         if(ISSET($_POST['upload'])){
  8.                 $file_name = $_FILES['image']['name'];
  9.                 $file_temp = $_FILES['image']['tmp_name'];
  10.                 $allowed_ext = array("jpeg", "jpg", "gif", "png");
  11.                 $exp = explode(".", $file_name);
  12.                 $ext = end($exp);
  13.                 $file=time().".".$ext;
  14.                 $path = "files/".$file;
  15.                 if(in_array($ext, $allowed_ext)){
  16.                         if(move_uploaded_file($file_temp, $path)){
  17.                                 mysqli_query($conn, "INSERT INTO `image` VALUES('', '$file', '$path')") or die(mysqli_error());
  18.                                 header('location:index.php');
  19.                         }
  20.                 }else{
  21.                         echo "<center><h3 class='text-danger'>Only image format can be upload</h3></center>";
  22.                 }
  23.         }
  24. ?>

Creating the Main Function

This code contains the main function of the application. The code will display the actual file size of an image file. To make this just copy and write these block of codes inside the text editor, then save it as get_imagesize.php
  1. <?php
  2.         if(ISSET($_REQUEST['file_name']))      
  3.        
  4.                 $image = "files/".$_REQUEST['file_name'];
  5.                
  6.                 $size = filesize($image);
  7.                
  8.                 $sizeInKB = ($size / 1024);
  9.                
  10.                
  11.                 $total_size = number_format($sizeInKB, 2);
  12.                
  13.                 echo "<script>alert('The image size is: ".$total_size."KB')</script>";
  14.                 echo "<script>window.location='index.php'</script>";
  15. ?>
There you have it we successfully created Get File Extension using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment