diff options
| author | Savetheinternet <savetheinternet@tinyboard.org> | 2011-05-20 17:19:27 +1000 |
|---|---|---|
| committer | Savetheinternet <savetheinternet@tinyboard.org> | 2011-05-20 17:19:27 +1000 |
| commit | d8301964b192c97e04fab81c80d90b2a19c49dc0 (patch) | |
| tree | a253024c422c121c64401e909244de8200a97579 /inc | |
| parent | e9a6b3fa58d1ec33bb20f83147643652074c3254 (diff) | |
better writing, file locking
Diffstat (limited to 'inc')
| -rw-r--r-- | inc/functions.php | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/inc/functions.php b/inc/functions.php index 97e2a802..b6b5f16e 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -211,6 +211,23 @@ } else return false; } + function file_write($path, $data) { + $fp = fopen($path, 'c'); + + // File locking + if(!flock($fp, LOCK_EX)) { + error('Unable to lock file!'); + } + + // Truncate file + ftruncate($fp, 0); + // Write data + fwrite($fp, $data); + flock($fp, LOCK_UN); + + fclose($fp); + } + function listBoards() { $query = query("SELECT * FROM `boards` ORDER BY `uri`") or error(db_error()); $boards = $query->fetchAll(); @@ -972,7 +989,7 @@ $content['pages'][$page-1]['selected'] = true; $content['btn'] = getPageButtons($content['pages']); $content['hidden_inputs'] = createHiddenInputs(); - @file_put_contents($filename, Element('index.html', $content)) or error("Couldn't write to file."); + file_write($filename, Element('index.html', $content)); if(isset($md5) && $md5 == md5_file($filename)) { break; @@ -997,7 +1014,7 @@ 'uri' => addslashes((!empty($uri) ? $config['uri_stylesheets'] : '') . $uri)); } - file_put_contents($config['file_script'], Element('main.js', Array( + file_write($config['file_script'], Element('main.js', Array( 'config' => $config, 'stylesheets' => $stylesheets ))); @@ -1269,7 +1286,7 @@ if($return) return $body; else - @file_put_contents($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $id), $body) or error("Couldn't write to file."); + file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $id), $body); } function rrmdir($dir) { |
