<?php
$handle=fopen('names.txt', 'w');
fwrite($handle, 'Alex'. "\n");
fwrite($handle, 'Zoloo');
fclose($handle);
echo 'Written';
?>
Reading a File
<?php
if(isset($_POST['name'])){
$name = $_POST['name'];
if(!empty($name)){
$handle = fopen('names.txt', 'a');
fwrite($handle, $name."\n");
fclose($handle);
$count = 1;
echo 'Current names in file: ';
$readin = file('names.txt');
$readin_count = count($readin);
foreach($readin as $fname){
echo trim($fname);
if($count< $readin_count){
echo ', ';
}
$count++;
}
} else {
echo 'Please type a name.';
}
}
?>
<form action = "some2.php" method="POST">
Name:<br>
<input type="text" name="name">
<input type="submit" name="Submit">
</form>
Appending a File
<?php
if(isset($_POST['name'])){
$name = $_POST['name'];
if(!empty($name)){
$handle = fopen('names.txt', 'a');
fwrite($handle, $name."\n");
fclose($handle);
} else {
echo 'Please type a name.';
}
}
?>
<form action = "some2.php" method="POST">
Name:<br>
<input type="text" name="name">
<input type="submit" name="Submit">
</form>
The explode Function with File Handling Example
<?php
$filename = 'names.txt';
$handle = fopen($filename, 'r');
// echo fread($handle, filesize($filename));
$datain = fread($handle, filesize($filename));
$names_array = explode(',', $datain);
foreach($names_array as $name){
echo $name.'<br>';
}
?>
The implode Function with File Handling Example
<?php
$names_array = array('Alex', 'Billy', 'Dale');
$string = implode('-', $names_array);
echo $string;
?>
No comments:
Post a Comment