1: <?php
2:
3: /**
4: * Class QFile: Handles reading and writing of files on the file system
5: */
6: class QFile {
7: /**
8: * Read the file from disk
9: *
10: * @param string $strFilePath Path of the file to be read
11: *
12: * @return string|mixed read data. Can return binary data
13: */
14: public static function readFile($strFilePath) {
15: $result = "";
16: $handle = fopen($strFilePath, "r");
17: while (!feof($handle)) {
18: $result .= fread($handle, 8000);
19: }
20: fclose($handle);
21:
22: return $result;
23: }
24:
25: /**
26: * Write data into file
27: *
28: * @param string $strFilePath Path of the file into which to write
29: * @param string $strContents The contents that should be written into the file
30: *
31: * @throws Exception
32: */
33: public static function writeFile($strFilePath, $strContents) {
34: $fileHandle = fopen($strFilePath, "w");
35: if (!$fileHandle) {
36: throw new Exception("Cannot open file for writing: " . $strFilePath);
37: }
38:
39: if (fwrite($fileHandle, $strContents, strlen($strContents)) === false) {
40: throw new Exception("Unable to write file: " . $strFilePath);
41: }
42: fclose($fileHandle);
43: }
44:
45: /**
46: * Will work despite of Windows ACLs bug
47: * NOTE: use a trailing slash for folders!!!
48: * See http://bugs.php.net/bug.php?id=27609 AND http://bugs.php.net/bug.php?id=30931
49: * Source: <http://www.php.net/is_writable#73596>
50:
51: */
52: public static function isWritable($path) {
53: // recursively return a temporary file path
54: if ($path{strlen($path) - 1} == '/') {
55: return self::isWritable($path . uniqid(mt_rand()) . '.tmp');
56: } elseif (is_dir($path)) {
57: return self::isWritable($path . '/' . uniqid(mt_rand()) . '.tmp');
58: }
59:
60: // check file for read/write capabilities
61: $rm = file_exists($path);
62: $handle = @fopen($path, 'a');
63:
64: if ($handle === false) {
65: return false;
66: }
67:
68: fclose($handle);
69:
70: if (!$rm) {
71: unlink($path);
72: }
73:
74: return true;
75: }
76: }