filesize from a String
Asked Answered
E

6

12

how can i get the "filesize" from a string in php?

I put the string in a mysql database as a blob and i need to store the size of the blob. My solution was to create a temp file and put the string into the temp file. now i can get the filesize from the "string". but that solution is not good...

greetings

Edwinedwina answered 18/8, 2010 at 10:31 Comment(0)
A
48

It depends. If you have mbstring function overloading enabled, the only call that will work will be mb_strlen($string, '8bit');. If it's not enabled, strlen($string) will work fine as well.

So, you can handle both cases like this:

if (function_exists('mb_strlen')) {
    $size = mb_strlen($string, '8bit');
} else {
    $size = strlen($string);
}
Aplanatic answered 18/8, 2010 at 10:46 Comment(6)
Care to explain the downvote? What's wrong or incomplete about this answer?Aplanatic
-1 Why use mb_strlen() at all when strlen() will always work? See my answer below.Tuddor
@TheodoreR.Smith: because, as I indicated in the answer, it won't always work. If mb_string.overload is on, strlen() will return the number of UTF-8 characters, not the number of bytes. So no, this is required...Aplanatic
In 7.2 this was deprecated, in version 8 it was removed. You should NOT use this method.Vassar
@Vassar what exactly are you talking about? What was removed in PHP 8? strlen()? mb_strlen()? function_exists()? All these methods are still in use, and were NOT deprecated neither removed yet.Murmuration
@Murmuration He's probably referring to mb_string,overloadRodriguez
E
7

SELECT length(field) FROM table

From the MySQL docs:

LENGTH(str)

Returns the length of the string str, measured in bytes. A multi-byte character counts as multiple bytes. This means that for a string containing five two-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.

Embrasure answered 18/8, 2010 at 10:41 Comment(0)
A
1
strlen()

before putting it into mysql, or in SQL:

LENGTH()

Notice that lenght can be various depending on character set. If you want to have real length in bytes use strlen(), if you want to have character count use mb_strlen() (if you have utf-8 encoding for example)

Aoristic answered 18/8, 2010 at 10:35 Comment(1)
strlen() can be flawed, see @ircmaxell's answerThompkins
P
0

If all you are storing is the string, then the size should be the length of your string times the number of bytes in the charset. So for Unicode that would be 2*strlen($string).

Postbox answered 18/8, 2010 at 10:34 Comment(2)
AFAIK only special chars have double size. So letter 'a' will have one byte, but letters like ó,ł,ż,ć,ś etc. will be 2-bytes longSoliloquy
Yup, UTF-8 is a variable-width encoding, characters can be between 1 and 4 bytes long. en.wikipedia.org/wiki/UTF-8Thompkins
I
0

strlen($string) is the best example for viewing the size(MB) of a string

strlen() doesnt actually return the number of elements in a string, but the number of bytes in it

example: echo(strlen('a■')); will return 4, because the black square character is made of 3 bytes, and the 'a' character is made of one.

Include answered 14/11, 2021 at 23:34 Comment(1)
It's great that you're providing an example to better explain, but otherwise your answer looks the same as the accepted one from 2010. If you agree, maybe you could edit that answer to add your example and delete this one?Ponderous
I
-3

use mb_strlen() as then you can tell it what type of encoding the string uses (if any) to get the size of it in bytes.

Insulin answered 18/8, 2010 at 10:35 Comment(6)
On second thought, sorry, but this is outright incorrect. mb_strlen() in conjunction with a multi-byte encoding is designed to return the size of the string in characters, not bytes. -1Thompkins
Upon further investigation rather than simply reading stuff (which I will make a mental note to do more in future), you can still use mb_strlen() to get the desired result by either omitting the second parameter, or setting the second parameter to be 8bit.Insulin
@gabe yup, it's even the only bullet-proof way as per @ircmaxell's answer (I wasn't aware of that either).Thompkins
Omitting the second parameters to MB_strlen wont work. It will fallback on the default encoding then (UTF-8) and not return as expecting....Aplanatic
@Aplanatic I tried it and omitting the second parameter returned the same result as strlen did. Maybe that's just the way I have my PHP set up though? Just wondering.Insulin
It will return the same result for ASCII characters. But if you have any UTF-8 characters in the string it won't return the correct result. mb_strlen counts the number of characters in the string for the current character set. The only way it'll return the number of bytes is to force the character set to a 1-byte-per-character charset. And since some characters are not valid in the majority of the 1 byte character sets, 8-bit is the only charset that will always return the correct byte length...Aplanatic

© 2022 - 2024 — McMap. All rights reserved.