@@ -188,24 +188,36 @@ function file_get_intbound_contents($url, $bindto_addr_family)
188188 return file_get_contents ($ url , false , $ stream_context );
189189}
190190
191- // Returns a file size limit in bytes based on the PHP upload_max_filesize
192- // and post_max_size
193- function file_upload_max_size ()
194- {
195- static $ max_size = -1 ;
191+ function file_upload_max_size () {
192+ // Retrieve the values from php.ini
193+ $ uploadMaxFileSize = ini_get ('upload_max_filesize ' );
194+ $ postMaxSize = ini_get ('post_max_size ' );
196195
197- if ( $ max_size < 0 ) {
198- // Start with post_max_size.
199- $ max_size = parse_size ( ini_get ( ' post_max_size ' ) );
196+ // Convert both values to bytes
197+ $ uploadMaxFileSizeBytes = convertToBytes ( $ uploadMaxFileSize );
198+ $ postMaxSizeBytes = convertToBytes ( $ postMaxSize );
200199
201- // If upload_max_size is less, then reduce. Except if upload_max_size is
202- // zero, which indicates no limit.
203- $ upload_max = parse_size (ini_get ('upload_max_filesize ' ));
204- if ($ upload_max > 0 && $ upload_max < $ max_size ) {
205- $ max_size = $ upload_max ;
206- }
200+ // Return the smaller of the two values
201+ return min ($ uploadMaxFileSizeBytes , $ postMaxSizeBytes );
202+ }
203+
204+ function convertToBytes ($ value ) {
205+ $ unit = strtoupper (substr ($ value , -1 ));
206+ $ bytes = (int )$ value ;
207+
208+ switch ($ unit ) {
209+ case 'G ' :
210+ $ bytes *= 1024 ** 3 ;
211+ break ;
212+ case 'M ' :
213+ $ bytes *= 1024 ** 2 ;
214+ break ;
215+ case 'K ' :
216+ $ bytes *= 1024 ;
217+ break ;
207218 }
208- return $ max_size ;
219+
220+ return $ bytes ;
209221}
210222
211223function parse_size ($ size )
0 commit comments