4 * Smarty write file plugin
7 * @subpackage PluginsInternal
12 * Smarty Internal Write File Class
14 class Smarty_Internal_Write_File {
16 * Writes file in a save way to disk
18 * @param string $_filepath complete filepath
19 * @param string $_contents file content
20 * @return boolean true
22 public static function writeFile($_filepath, $_contents, $smarty)
24 $old_umask = umask(0);
25 $_dirpath = dirname($_filepath);
26 // if subdirs, create dir structure
27 if ($_dirpath !== '.' && !file_exists($_dirpath)) {
28 mkdir($_dirpath, $smarty->_dir_perms, true);
30 // write to tmp file, then move to overt file lock race condition
31 $_tmp_file = tempnam($_dirpath, 'wrt');
33 if (!($fd = @fopen($_tmp_file, 'wb'))) {
34 $_tmp_file = $_dirpath . DS . uniqid('wrt');
35 if (!($fd = @fopen($_tmp_file, 'wb'))) {
36 throw new SmartyException("unable to write file {$_tmp_file}");
41 fwrite($fd, $_contents);
44 // remove original file
45 if (file_exists($_filepath))
48 rename($_tmp_file, $_filepath);
49 // set file permissions
50 chmod($_filepath, $smarty->_file_perms);