[PATCH] use file_get_contents instead fread($fp, filesize($filename))
| From: | Antony Dovgal | Date: | Tue, 06 Jul 2004 14:03:25 +0000 |
| Subject: | [PATCH] use file_get_contents instead fread($fp, filesize($filename)) | ||
| Groups: | php.smarty.dev | ||
| Request: | Send a blank email to php-smarty-dev+get-2440@lists.php.net to get a copy of this message | ||
Hi!
Small patch, which removes all fopen() - fread($fp, filesize($filename))
- fclose() cases, replacing them with one file_get_contents() call.
Moreover, this fixes problem with bug http://bugs.php.net/29023, which
prevents Smarty from working properly.
Regardless of the fact that this bug will be fixed (I hope), this
fopen-fread-filesize-fclose mess can be safely replaced by one
file_get_contents() calls.
Please, review it.
---
WBR,
Antony Dovgal aka tony2001
tony2001@phpclub.net || antony@dovgal.com
Index: Config_File.class.php =================================================================== RCS file: /repository/smarty/libs/Config_File.class.php,v retrieving revision 1.71 diff -u -r1.71 Config_File.class.php --- Config_File.class.php 17 Jun 2004 08:55:09 -0000 1.71 +++ Config_File.class.php 6 Jul 2004 13:50:57 -0000 @@ -234,14 +234,12 @@ $config_file = $file_name; ini_set('track_errors', true); - $fp = @fopen($config_file, "r"); - if (!is_resource($fp)) { + if (!is_readable($config_file)) { $this->_trigger_error_msg("Could not open config file '$config_file'"); return false; } - $contents = ($size = filesize($config_file)) ? fread($fp, $size) : ''; - fclose($fp); + $contents = ($size = filesize($config_file)) ? file_get_contents($config_file) : ''; $this->_config_data[$config_file] = $this->parse_contents($contents); return true; Index: Smarty.class.php =================================================================== RCS file: /repository/smarty/libs/Smarty.class.php,v retrieving revision 1.492 diff -u -r1.492 Smarty.class.php --- Smarty.class.php 1 Jul 2004 15:48:40 -0000 1.492 +++ Smarty.class.php 6 Jul 2004 13:50:57 -0000 @@ -1705,9 +1705,8 @@ */ function _read_file($filename) { - if ( file_exists($filename) && ($fd = @fopen($filename, 'rb')) ) { - $contents = ($size = filesize($filename)) ? fread($fd, $size) : ''; - fclose($fd); + if ( is_readable($filename) ) { + $contents = ($size = filesize($filename)) ? file_get_contents($filename) : ''; return $contents; } else { return false;
Index: Config_File.class.php =================================================================== RCS file: /repository/smarty/libs/Config_File.class.php,v retrieving revision 1.71 diff -u -r1.71 Config_File.class.php --- Config_File.class.php 17 Jun 2004 08:55:09 -0000 1.71 +++ Config_File.class.php 6 Jul 2004 13:50:57 -0000 @@ -234,14 +234,12 @@ $config_file = $file_name; ini_set('track_errors', true); - $fp = @fopen($config_file, "r"); - if (!is_resource($fp)) { + if (!is_readable($config_file)) { $this->_trigger_error_msg("Could not open config file '$config_file'"); return false; } - $contents = ($size = filesize($config_file)) ? fread($fp, $size) : ''; - fclose($fp); + $contents = ($size = filesize($config_file)) ? file_get_contents($config_file) : ''; $this->_config_data[$config_file] = $this->parse_contents($contents); return true; Index: Smarty.class.php =================================================================== RCS file: /repository/smarty/libs/Smarty.class.php,v retrieving revision 1.492 diff -u -r1.492 Smarty.class.php --- Smarty.class.php 1 Jul 2004 15:48:40 -0000 1.492 +++ Smarty.class.php 6 Jul 2004 13:50:57 -0000 @@ -1705,9 +1705,8 @@ */ function _read_file($filename) { - if ( file_exists($filename) && ($fd = @fopen($filename, 'rb')) ) { - $contents = ($size = filesize($filename)) ? fread($fd, $size) : ''; - fclose($fd); + if ( is_readable($filename) ) { + $contents = ($size = filesize($filename)) ? file_get_contents($filename) : ''; return $contents; } else { return false;