libs/sysplugins/smarty_internal_write_file.php Quellcode

smarty_internal_write_file.php
gehe zur Dokumentation dieser Datei
1 <?php
17 {
28  public function writeFile($_filepath, $_contents, Smarty $smarty)
29  {
30  $_error_reporting = error_reporting();
31  error_reporting($_error_reporting & ~E_NOTICE & ~E_WARNING);
32  if ($smarty->_file_perms !== null) {
33  $old_umask = umask(0);
34  }
35 
36  $_dirpath = dirname($_filepath);
37  // if subdirs, create dir structure
38  if ($_dirpath !== '.' && !file_exists($_dirpath)) {
39  mkdir($_dirpath, $smarty->_dir_perms === null ? 0777 : $smarty->_dir_perms, true);
40  }
41 
42  // write to tmp file, then move to overt file lock race condition
43  $_tmp_file = $_dirpath . DS . str_replace(array('.', ','), '_', uniqid('wrt', true));
44  if (!file_put_contents($_tmp_file, $_contents)) {
45  error_reporting($_error_reporting);
46  throw new SmartyException("unable to write file {$_tmp_file}");
47  }
48 
49  /*
50  * Windows' rename() fails if the destination exists,
51  * Linux' rename() properly handles the overwrite.
52  * Simply unlink()ing a file might cause other processes
53  * currently reading that file to fail, but linux' rename()
54  * seems to be smart enough to handle that for us.
55  */
57  // remove original file
58  if (is_file($_filepath)) {
59  @unlink($_filepath);
60  }
61  // rename tmp file
62  $success = @rename($_tmp_file, $_filepath);
63  } else {
64  // rename tmp file
65  $success = @rename($_tmp_file, $_filepath);
66  if (!$success) {
67  // remove original file
68  if (is_file($_filepath)) {
69  @unlink($_filepath);
70  }
71  // rename tmp file
72  $success = @rename($_tmp_file, $_filepath);
73  }
74  }
75  if (!$success) {
76  error_reporting($_error_reporting);
77  throw new SmartyException("unable to write file {$_filepath}");
78  }
79  if ($smarty->_file_perms !== null) {
80  // set file permissions
81  chmod($_filepath, $smarty->_file_perms);
82  umask($old_umask);
83  }
84  error_reporting($_error_reporting);
85 
86  return true;
87  }
88 }




Korrekturen, Hinweise und Ergänzungen

Bitte scheuen Sie sich nicht und melden Sie, was auf dieser Seite sachlich falsch oder irreführend ist, was ergänzt werden sollte, was fehlt usw. Dazu bitte oben aus dem Menü Seite den Eintrag Support Forum wählen. Es ist eine kostenlose Anmeldung erforderlich, um Anmerkungen zu posten. Unpassende Postings, Spam usw. werden kommentarlos entfernt.