filesize from a String

前端 未结 5 574
自闭症患者
自闭症患者 2021-02-07 21:13

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

相关标签:
5条回答
  • 2021-02-07 21:35

    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.

    0 讨论(0)
  • 2021-02-07 21:35

    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.

    0 讨论(0)
  • 2021-02-07 21:36

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

    0 讨论(0)
  • 2021-02-07 21:49
    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)

    0 讨论(0)
  • 2021-02-07 21:51

    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);
    }
    
    0 讨论(0)
提交回复
热议问题