note 94643 added to function.fopen
| From: | jbud at jbud dot co dot cc | Date: | Mon, 16 Nov 2009 15:41:57 +0000 |
| Subject: | note 94643 added to function.fopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162956@lists.php.net to get a copy of this message | ||
This is a function for reading or writing to a text file...
The function has three modes, READ, WRITE, and READ/WRITE... READ, returns the text of a file, or
returns FALSE if the file isn't found. WRITE creates or overrides a file with the text from the
$input string, WRITE returns TRUE or FALSE depending on success or failure. READ/WRITE reads a text
file, then writes the string from $input to the end of the file, like WRITE, it returns TRUE or
FALSE depending on success or failure.
<?php
function openFile($file, $mode, $input) {
if ($mode == "READ") {
if (file_exists($file)) {
$handle = fopen($file, "r");
$output = fread($handle, filesize($file));
return $output; // output file text
} else {
return false; // failed.
}
} elseif ($mode == "WRITE") {
$handle = fopen($file, "w");
if (!fwrite($handle, $input)) {
return false; // failed.
} else {
return true; //success.
}
} elseif ($mode == "READ/WRITE") {
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);
}
?>
EXAMPLE CALLS:
<php
openFile2("files/text.txt", "WRITE", "Hello World!");
echo openFile2("files/text.txt", "READ"); // OUTPUT > Hello World!
openFile2("files/text.txt", "READ/WRITE", "!!");
echo openFile2("files/text.txt", "READ"); // OUTPUT > Hello World!!!
?>
----
Server IP: 69.147.83.197
Probable Submitter: 71.237.144.226
----
Manual Page -- http://www.php.net/manual/en/function.fopen.php
Edit -- https://master.php.net/note/edit/94643
Del: integrated -- https://master.php.net/note/delete/94643/integrated
Del: useless -- https://master.php.net/note/delete/94643/useless
Del: bad code -- https://master.php.net/note/delete/94643/bad+code
Del: spam -- https://master.php.net/note/delete/94643/spam
Del: non-english -- https://master.php.net/note/delete/94643/non-english
Del: in docs -- https://master.php.net/note/delete/94643/in+docs
Del: other reasons-- https://master.php.net/note/delete/94643
Reject -- https://master.php.net/note/reject/94643
Search -- https://master.php.net/manage/user-notes.php