Element
01-04-2006, 05:13 AM
I wrote these simple, yet usefull functions for file handling. They were for someone, but he didn't want them, so I'm sharing them here. If you have anything to add to them, feel free to post the upgrade/modification/etc.
If a moderator,/administrator feels this isn't usefull, and deletes it, please PM with your reason why, it makes it easier for future referense on what to post of mine.
<?php
function writeToFile($handle, $data, $openAs = "w") {
if(file_exists($handle) && is_file($handle) && is_writable($handle)) {
if($openAs !== "r" && $handle = @fopen($handle, $openAs)) {
if(@fputs($data, $handle)) {
@fclose($handle);
return true;
} else {
@fclose($handle);
echo "<b>fputs()</b> Error writing to file, or the setting was for reading!";
return false;
}
} else {
echo "<b>fopen()</b> Error opening file!";
return false;
}
} else {
echo "<b>is_writable()</b> The file is either not writable, or doesn't exist!";
return false;
}
}
function readFromFile($handle, $array = true) {
if(is_readable($handle)) {
if($file = @fopen($handle, "r")) {
if($array) {
$return = array();
while(!feof($file)) {
$return[] = fgets($file);
}
@fclose($file);
return $return;
} else {
$return = "";
while(!feof($file)) {
$return .= fgets($file);
}
@fclose($file);
return $return;
}
} else {
echo "<b>fopen(), flock()</b> The file could not be opened and locked!";
return false;
}
} else {
echo "<b>is_readable()</b> The file is either not readable, or doesn't exist!";
return false;
}
}
// This example was for the useer, a compact piece of code to replace a file.
$handle = "/home/user/public_html/data/archive1-a-1.dat";
@unlink($handle);
$data = "Temporarily unavailable.";
if(writeToFile($handle, $data, "a+")) {
echo "File replacement, complete.";
} else {
echo "File replacement failed, see above error.";
}
$file_contents = readFromFile($handle, false); // Read file into a string
echo $file_contents;
$file_contents = readFromFile($handle); // Read file into array, like file() function
print_r ($file_contents);
?>
Planning on implementing the usage of ports, in the opening of files, for remote files, etc.
If a moderator,/administrator feels this isn't usefull, and deletes it, please PM with your reason why, it makes it easier for future referense on what to post of mine.
<?php
function writeToFile($handle, $data, $openAs = "w") {
if(file_exists($handle) && is_file($handle) && is_writable($handle)) {
if($openAs !== "r" && $handle = @fopen($handle, $openAs)) {
if(@fputs($data, $handle)) {
@fclose($handle);
return true;
} else {
@fclose($handle);
echo "<b>fputs()</b> Error writing to file, or the setting was for reading!";
return false;
}
} else {
echo "<b>fopen()</b> Error opening file!";
return false;
}
} else {
echo "<b>is_writable()</b> The file is either not writable, or doesn't exist!";
return false;
}
}
function readFromFile($handle, $array = true) {
if(is_readable($handle)) {
if($file = @fopen($handle, "r")) {
if($array) {
$return = array();
while(!feof($file)) {
$return[] = fgets($file);
}
@fclose($file);
return $return;
} else {
$return = "";
while(!feof($file)) {
$return .= fgets($file);
}
@fclose($file);
return $return;
}
} else {
echo "<b>fopen(), flock()</b> The file could not be opened and locked!";
return false;
}
} else {
echo "<b>is_readable()</b> The file is either not readable, or doesn't exist!";
return false;
}
}
// This example was for the useer, a compact piece of code to replace a file.
$handle = "/home/user/public_html/data/archive1-a-1.dat";
@unlink($handle);
$data = "Temporarily unavailable.";
if(writeToFile($handle, $data, "a+")) {
echo "File replacement, complete.";
} else {
echo "File replacement failed, see above error.";
}
$file_contents = readFromFile($handle, false); // Read file into a string
echo $file_contents;
$file_contents = readFromFile($handle); // Read file into array, like file() function
print_r ($file_contents);
?>
Planning on implementing the usage of ports, in the opening of files, for remote files, etc.