views:

49

answers:

5

hello!

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

A: 

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

DrDipshit
AFAIK only special chars have double size. So letter 'a' will have one byte, but letters like ó,ł,ż,ć,ś etc. will be 2-bytes long
killer_PL
Oh, I did not know that. Cheers.
DrDipshit
Yup, UTF-8 is a variable-width encoding, characters can be between 1 and 4 bytes long. http://en.wikipedia.org/wiki/UTF-8
Pekka
A: 

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.

gabe3886
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. -1
Pekka
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.
gabe3886
@gabe yup, it's even the *only* bullet-proof way as per @ircmaxell's answer (I wasn't aware of that either).
Pekka
Omitting the second parameters to MB_strlen wont work. It will fallback on the default encoding then (UTF-8) and not return as expecting....
ircmaxell
Just use strlen...its faster and just as accurate as mb_strlen...
Stephen
@ircmaxell 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.
gabe3886
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...
ircmaxell
+1  A: 
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)

killer_PL
strlen() can be flawed, see @ircmaxell's answer
Pekka
+2  A: 

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.

Kwebble
+1 this is the correct answer on the mySQL end
Pekka
A: 

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);
}
ircmaxell
thank you! it works!
3x3cut3r
+1 this is the correct answer
Pekka
Care to explain the downvote? What's wrong or incomplete about this answer?
ircmaxell