Added Canvas 1.1.0, originally not under SCM so no historical development records...
[canvas.git] / library / smarty / internals / core.write_file.php
blob333b7621b3cff992249e45650b8ae32c09a5950b
1 <?php
2 /**
3 * Smarty plugin
4 * @package Smarty
5 * @subpackage plugins
6 */
8 /**
9 * write out a file to disk
11 * @param string $filename
12 * @param string $contents
13 * @param boolean $create_dirs
14 * @return boolean
16 function smarty_core_write_file($params, &$smarty)
18 $_dirname = dirname($params['filename']);
20 if ($params['create_dirs']) {
21 $_params = array('dir' => $_dirname);
22 require_once(SMARTY_CORE_DIR . 'core.create_dir_structure.php');
23 smarty_core_create_dir_structure($_params, $smarty);
26 // write to tmp file, then rename it to avoid
27 // file locking race condition
28 $_tmp_file = tempnam($_dirname, 'wrt');
30 if (!($fd = @fopen($_tmp_file, 'wb'))) {
31 $_tmp_file = $_dirname . DIRECTORY_SEPARATOR . uniqid('wrt');
32 if (!($fd = @fopen($_tmp_file, 'wb'))) {
33 $smarty->trigger_error("problem writing temporary file '$_tmp_file'");
34 return false;
38 fwrite($fd, $params['contents']);
39 fclose($fd);
41 // Delete the file if it allready exists (this is needed on Win,
42 // because it cannot overwrite files with rename()
43 if (file_exists($params['filename'])) {
44 @unlink($params['filename']);
46 @rename($_tmp_file, $params['filename']);
47 @chmod($params['filename'], $smarty->_file_perms);
49 return true;
52 /* vim: set expandtab: */