how to get image file size with php

2020-05-07 03:49发布

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" }

标签: php
4条回答
等我变得足够好
2楼-- · 2020-05-07 04:16

Do you need the filesize() function?

查看更多
疯言疯语
3楼-- · 2020-05-07 04:19

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

查看更多
够拽才男人
4楼-- · 2020-05-07 04:23

This should work:

$filename = 'somefile.txt';
echo filesize($filename) . ' bytes';
查看更多
Emotional °昔
5楼-- · 2020-05-07 04:30

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)) {


    }
    }
查看更多
登录 后发表回答