andreea115
andreea115

Reputation: 53

how to get image file size with php

I am trying to find a PHP function that will allow me to determine the file size of images uploaded. i.e. I want to block images over a certain size.

I tried using the getimagesize(); function but this only seem to give the width and height of an image.

I have enclosed below the array values returned from the getimagesize function. Unless i am mistaken, there is nothing in this array that relates to the actual file size

array(6) { [0]=> int(800) [1]=> int(800) [2]=> int(3) [3]=> string(24) "width="800" height="800"" ["bits"]=> int(music) ["mime"]=> string(9) "image/png" }

Upvotes: 0

Views: 1582

Answers (5)

Jodyshop
Jodyshop

Reputation: 664

There is an easy approach for that, first, determine the image URL, then check it's size as follows:

$image = get_headers("full-url-to-image", 1);
$bytes = $image["Content-Length"];
echo "$bytes";

That will echo image size in bytes for example:

length="12113"

Upvotes: 0

Vikas Umrao
Vikas Umrao

Reputation: 2615

You can try code like this:

if($_FILES['image']['size'] <= 2097152 )
    {
    $uploaddir = $_SERVER['DOCUMENT_ROOT'].'/writereaddata/'; // destination folder



    if (move_uploaded_file($_FILES['image']['tmp_name'], $uploaddir)) {


    }
    }

Upvotes: 1

STT LCU
STT LCU

Reputation: 4330

You're being deceived by the "image" part of your question. How do you determine the file size of a generic file? why would an image be different?

Since images are files too, you should use the filesize() function provided by PHP

Upvotes: 4

Companjo
Companjo

Reputation: 1792

This should work:

$filename = 'somefile.txt';
echo filesize($filename) . ' bytes';

Upvotes: 1

vanamerongen
vanamerongen

Reputation: 837

Do you need the filesize() function?

Upvotes: 0

Related Questions