2013-05-08 52 views
4

當我運行這段代碼:爲什麼我會從`filesize`中獲得如此精確的結果?

<?php 
$handle = fopen('/tmp/lolwut', 'w') or die("Cannot open File");  
fwrite($handle, "1234567890"); 
fclose($handle); 

print_r(filesize('/tmp/lolwut')); 
?> 

我得到的結果10,這是文件中的字符數正確。

但是,因爲文件系統塊比這個大得多,所以我期望文件大小被「四捨五入」到512字節甚至1KB。爲什麼不是?

+0

_this問題被設計爲解決[誤解](http://stackoverflow.com/questions/16435318/fread-cannot-read-new-line-added/16435725?noredirect= 1#comment23572278_16435725)._ – 2013-05-08 10:19:54

+0

對com的好問題。 wiki,但是1.你的鏈接與這個問題有什麼關係? (這是關於緩存)和2.不會有更好的標題*爲什麼'文件大小'返回一個比文件使用更小的尺寸?*或類似的東西? – dtech 2013-05-08 10:28:24

+1

@dtech:這是一個評論鏈接。閱讀評論。 – 2013-05-08 10:34:28

回答

8

不要將「文件大小」混淆爲「磁盤上的文件大小」; PHP's filesize function給你前者,而不是後者。

雖然沒有明確記載這樣,filesize是在stat條件基本落實,並在Linux上stat makes a distinction between filesize and "file size on disk"

所有這些系統調用的返回stat結構,它包含以下字段:

struct stat { 
    // [...] 
    off_t  st_size; /* total size, in bytes */ 
    blksize_t st_blksize; /* blocksize for file system I/O */ 
    blkcnt_t st_blocks; /* number of 512B blocks allocated */ 
    // [...] 
}; 

您期待的值爲st_blocks * st_blksize,但「true」文件大小爲st_size可用gardless。

This appears to be the case on Windows, too。)

相關問題