JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fdb works, added fault-tollerent read_wole_file
[wfpl.git] / file.php
1 <?php
2
3 #  Copyright (C) 2007 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # This exists because file_get_contents() is not documented well. (It says that
23 # the second parameter is optional, but does not specify the default behavior.)
24 function read_whole_file($name) {
25         $fd = fopen($name, 'r');
26         if($fd === false) {
27                 die("Failed to read file: '$name'");
28         }
29         $file_data = fread($fd, filesize($name));
30         fclose($fd);
31         return $file_data;
32 }
33
34 # This exists because file_put_contents() is not included in PHP4.
35 function write_whole_file($name, $data) {
36         $fd = fopen($name, 'w');
37         if($fd === false) {
38                 die("Failed to read file: '$name'");
39         }
40         fwrite($fd, $data);
41         fclose($fd);
42 }
43
44 function read_whole_file_or_false($name) {
45         if(!file_exists($name)) {
46                 return false;
47         }
48         return read_whole_file($name);
49 }
50
51 ?>