PHP Filesize Reporting Old Size

PHP filesize reporting old size

On Linux based systems, data fetched by filesize() is "statcached".

Try calling clearstatcache(); before the filesize call.

filesize not update after change file

As the manual puts it:

Note: The results of this function are cached. See clearstatcache() for more details.

Can you trust file size given by $_FILES array in PHP?

If the file is uploaded correctly and everything is fine, you can use the info provided by PHP superglobal $_FILES. Using filesize() adds small overhead since OS needs to inspect the file for the size. It's up to you, but checking PHP source on how it does all this indicates clearly that it correctly calculates the file size in the HTTP multipart request. Basically, you'd be doing the same job again if you were to filesize() the file.

The reason you can trust this directly from superglobal variable is the fact that multipart requests supply a boundary between which the data resides. By definition, it's not possible to obtain corrupt data if the protocol for extracting the data isn't followed. In other words, it means that browser sends a "delimiter" and PHP simply finds it and starts checking the text for data between that delimiter. To do this, it accurately allocates required memory and it can immediately cache the number allocated - and that number is the file size. If anything is wrong along the way, you will get errors. Therefore, if the file uploaded correctly, the information about the size is trusted.

PHP delete first row of file till threshold is reached

The results of filesize are cached. You need to call clearstatcache() after updating the file.

https://www.php.net/manual/en/function.clearstatcache.php

How to get directory size in PHP

The following are other solutions offered elsewhere:

If on a Windows Host:

<?
$f = 'f:/www/docs';
$obj = new COM ( 'scripting.filesystemobject' );
if ( is_object ( $obj ) )
{
$ref = $obj->getfolder ( $f );
echo 'Directory: ' . $f . ' => Size: ' . $ref->size;
$obj = null;
}
else
{
echo 'can not create object';
}
?>

Else, if on a Linux Host:

<?
$f = './path/directory';
$io = popen ( '/usr/bin/du -sk ' . $f, 'r' );
$size = fgets ( $io, 4096);
$size = substr ( $size, 0, strpos ( $size, "\t" ) );
pclose ( $io );
echo 'Directory: ' . $f . ' => Size: ' . $size;
?>

Calculate image file size before saving to Disk in PHP

There is no way to calculate the size before saving it to the disk. I advice you create some kind of temporary folder to save the image, then check the size and if it is what you want move it to the folder you see fit, if you dont want to save it, delete it from the temporary folder.



Related Topics



Leave a reply



Submit