1

file_put_contentsを使用してファイルをアップロードしています。* move_uploaded_file *のようにファイルサイズを計算する方法はありますか?文字列の長さとfile_sizeは2つの異なるものだと思います。

4

2 に答える 2

5

file_put_contentsの戻り値に関連するドキュメントによると:

この関数は、ファイルに書き込まれたバイト数を返します。失敗した場合はFALSEを返します。

したがって、次のようなことができるはずです。

$filesize = file_put_contents($myFile, $someData);
于 2012-04-16T20:52:15.507 に答える
1

filesize()ファイルのサイズを計算するという関数があります。ファイルパスをパラメータとして渡します。

$filesize = filesize("myfiles/file.txt");

次に、次のような関数を使用してファイルサイズをフォーマットし、よりユーザーフレンドリーにすることができます。

function format_bytes($a_bytes) {
    if ($a_bytes < 1024) {
        return $a_bytes .' B';
    } elseif ($a_bytes < 1048576) {
        return round($a_bytes / 1024, 2) .' KB';
    } elseif ($a_bytes < 1073741824) {
        return round($a_bytes / 1048576, 2) . ' MB';
    } elseif ($a_bytes < 1099511627776) {
        return round($a_bytes / 1073741824, 2) . ' GB';
    } elseif ($a_bytes < 1125899906842624) {
        return round($a_bytes / 1099511627776, 2) .' TB';
    } elseif ($a_bytes < 1152921504606846976) {
        return round($a_bytes / 1125899906842624, 2) .' PB';
    } elseif ($a_bytes < 1180591620717411303424) {
        return round($a_bytes / 1152921504606846976, 2) .' EB';
    } elseif ($a_bytes < 1208925819614629174706176) {
        return round($a_bytes / 1180591620717411303424, 2) .' ZB';
    } else {
        return round($a_bytes / 1208925819614629174706176, 2) .' YB';
    }
}
于 2012-04-16T21:02:55.207 に答える