diff --git a/htdocs/lib/files.lib.php b/htdocs/lib/files.lib.php index 316c0c2e55d..01d23baa662 100644 --- a/htdocs/lib/files.lib.php +++ b/htdocs/lib/files.lib.php @@ -301,5 +301,69 @@ function dol_is_file($pathoffile) $newpathoffile=dol_osencode($pathoffile); return is_file($newpathoffile); } +/** + * Reading or writing to a text file + * + * @param $file File to open or create + * @param $mode Mode to open file : r = returns the text of a file, or returns FALSE if the file isn't found + * w = creates or overrides a file with the text + * r+ = reads a text file, then writes the text to the end of the file + * @param $input String of text + * @return boolean True if ok, false if error + */ +function dol_openfile($file, $mode, $input) +{ + if ($mode == "r") + { + if (file_exists($file)) + { + $handle = fopen($file, "r"); + $output = fread($handle, filesize($file)); + return $output; // output file text + } + else + { + return false; // failed. + } + } + elseif ($mode == "w") + { + $handle = fopen($file, "w"); + if (!fwrite($handle, $input)) + { + return false; // failed. + } + else + { + return true; //success. + } + } + elseif ($mode == "r+") + { + if (file_exists($file) && isset($input)) + { + $handle = fopen($file, "r+"); + $read = fread($handle, filesize($file)); + $data = $read.$input; + if (!fwrite($handle, $data)) + { + return false; // failed. + } + else + { + return true; // success. + } + } + else + { + return false; // failed. + } + } + else + { + return false; // failed. + } + fclose($handle); +} ?>