<?php
// DB credentials.
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','myimage');
// Establish database connection.
try
{
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
}
catch (PDOException $e)
{
exit("Error: " . $e->getMessage());
}
?>
index.php
<?php
session_start();
error_reporting(0);
include "config.php";
// Uploads files
if(isset($_POST['save'])) { // if save button on the form is clicked
// name of the uploaded file
$filename = $_FILES['myfile']['name'];
// destination of the file on the server
$destination = 'media/' . $filename;
// the physical file on a temporary uploads directory on the server
$file = $_FILES['myfile']['tmp_name'];
$size = $_FILES['myfile']['size'];
if ($_FILES['myfile']['size'] > 307200)
{
// file shouldn't be larger than 1Megabyte
echo "<script>alert('File too large! should not be larger than 300kb');</script>";
}
else {
// move the uploaded (temporary) file to the specified destination
if (move_uploaded_file($file, $destination)) {
$sql = "INSERT INTO imagesize (filename,size) VALUES ('$filename', '$size')";
if (mysqli_query($conn, $sql))
{
echo "<script>alert('Image uploaded successfully');</script>";
} else {
echo "<script>alert('Failed to upload file.');</script>";
}
}
}
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>How to restrict image size upload in php</title>
<!--Bootstrap -->
<link rel="stylesheet" href="css/bootstrap.css" type="text/css">
</head>
<body>
<div class="container">
<p style="font-size: 23px;">How to restrict image size upload in php</p>
<form class="form-horizontal row-fluid" name="insertimage" method="post" enctype="multipart/form-data">
<div class="control-group">
<label class="control-label" for="basicinput">Upload Image(<span style="color: red;">Image Size should not be larger than 300 kb </span> )</label>
<div> </div>
<div class="controls">
<input type="file" name="myfile" id="myfile" value="" class="span8 tip" required>
</div>
</div>
<br>
<div class="form-group row">
<div class="col-12">
<button type="submit" class="btn btn-success" name="save">
<i class="fa fa-plus "></i> Upload
</button>
</div>
</div>
</form>
<table id="" class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Image</th>
<th>Image Name</th>
<th>Image Size</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * from imagesize order by id desc";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{
?>
<tr>
<td><?php echo $cnt;?></td>
<td><img class="" src="media/<?php echo $result->filename;?>" alt="Image" width="100" height="80"></td>
<td><?php echo ($result->filename) ?></td>
<?php
$size=(($result->size)/1024) ;
$param = number_format($size, 1, '.', '');
?>
<td><?php echo ($param);?> kb</td>
</tr>
<?php
$cnt=$cnt+1;
}
} ?>
</tbody>
</table>
</div>
</body>
</html>
Download