u can access the file,, read the file or write to the file
|
|||||||||||||||||||||||||||
file.php
|
|||||||||||||||||||||||||||
<?php
|
|||||||||||||||||||||||||||
fopen()
|
using fopen we created a file
|
||||||||||||||||||||||||||
?>
|
|||||||||||||||||||||||||||
<?php
|
|||||||||||||||||||||||||||
fopen('users.txt', 'w');
|
w means writing into file
|
||||||||||||||||||||||||||
?>
|
user.txt- file name we created
|
||||||||||||||||||||||||||
<?php
|
|||||||||||||||||||||||||||
fopen('users.txt', 'w');
|
if u open the text file before opened this file.php page, it
blanks- nothing is writed
|
||||||||||||||||||||||||||
fwrite(fopen('users.txt', 'w'),'rafique');
|
if u open the text file after open this file , it will write
into text file as rafique
|
||||||||||||||||||||||||||
?>
|
rafique
|
||||||||||||||||||||||||||
if we want to write two more words
|
|||||||||||||||||||||||||||
<?php
|
|||||||||||||||||||||||||||
$file = fopen('users.txt', 'w');
|
writed the fopen function to a avariable
|
||||||||||||||||||||||||||
fwrite($file, 'rafique');
|
write rafique into file using fwrite
|
||||||||||||||||||||||||||
fwrite($file, 'saneer');
|
rafiquesaneer
|
||||||||||||||||||||||||||
?>
|
|||||||||||||||||||||||||||
write to a next line
|
|||||||||||||||||||||||||||
fwrite($file, 'rafique' ."\n");
|
rafique
|
||||||||||||||||||||||||||
fwrite($file, 'saneer');
|
saneer
|
||||||||||||||||||||||||||
appendint to a file with php
|
|||||||||||||||||||||||||||
lesson 29
|
|||||||||||||||||||||||||||
<?php
|
|||||||||||||||||||||||||||
$file = fopen('users.txt', 'w');
|
opened the file
|
||||||||||||||||||||||||||
fwrite($file, 'rafique' ."\n");
|
write the rafique
|
||||||||||||||||||||||||||
fwrite($file, 'saneer');
|
write the saneer
|
||||||||||||||||||||||||||
fclose($file);
|
close thefile
|
||||||||||||||||||||||||||
echo 'writter';
|
written shows for customer purpes
|
||||||||||||||||||||||||||
?>
|
|||||||||||||||||||||||||||
how to append the text every time they writes
|
|||||||||||||||||||||||||||
<?php
|
|||||||||||||||||||||||||||
if (isset ($_POST ['name'])){
|
if subumit
|
||||||||||||||||||||||||||
$name = $_post['name'];
|
name saved to name vairable
|
||||||||||||||||||||||||||
if (!empty($name)){
|
if it is not empty, created names,txt file and a means add means
any data entered by user
|
||||||||||||||||||||||||||
w means every time write , overwrites only one line, a means
append
|
|||||||||||||||||||||||||||
$file = fopen ('names.txt', 'a');
|
aftercreated the text , what will do this , express by second
argument
|
||||||||||||||||||||||||||
fwrite($file, $name."\n");
|
write text to file
|
||||||||||||||||||||||||||
fclose ($file);
|
after write close the file
|
||||||||||||||||||||||||||
echo 'written';
|
we wriiten print in this place
|
||||||||||||||||||||||||||
} else {
|
|||||||||||||||||||||||||||
echo 'please enter a name';}
|
if not written any name write pleas enter a name
|
||||||||||||||||||||||||||
}
|
|||||||||||||||||||||||||||
?>
|
|||||||||||||||||||||||||||
<form action="file.php" method="POST">
|
method is post, after send ,it returns to same filephp page
|
||||||||||||||||||||||||||
<input type="text" name="name'>
|
after submit
|
||||||||||||||||||||||||||
<input type="submit" value="add">
|
|||||||||||||||||||||||||||
</form>
|
|||||||||||||||||||||||||||
leeson 30
|
|||||||||||||||||||||||||||
reading a file with php
|
|||||||||||||||||||||||||||
<?php
|
|||||||||||||||||||||||||||
echo 'h3> current names: </h3>
|
|||||||||||||||||||||||||||
$readin = file('names.txt');
|
text file saved to variable
|
||||||||||||||||||||||||||
foreach( $readin as $newname){
|
onvert that name to another name and read each line using for
each and print each line
|
||||||||||||||||||||||||||
echo $newname. ',';
|
name, name, name,
|
||||||||||||||||||||||||||
}
|
how It prints comma, because we used comma, with space also we
used
|
||||||||||||||||||||||||||
if (isset ($_POST ['name'])){
|
|||||||||||||||||||||||||||
$name = $_post['name'];
|
|||||||||||||||||||||||||||
if (!empty($name)){
|
|||||||||||||||||||||||||||
$file = fopen ('names.txt', 'a');
|
|||||||||||||||||||||||||||
fwrite($file, $name."\n");
|
|||||||||||||||||||||||||||
fclose ($file);
|
|||||||||||||||||||||||||||
echo 'written';
|
|||||||||||||||||||||||||||
} else {
|
|||||||||||||||||||||||||||
echo 'please enter a name';}
|
|||||||||||||||||||||||||||
}
|
|||||||||||||||||||||||||||
?>
|
|||||||||||||||||||||||||||
<form action="file.php" method="POST">
|
|||||||||||||||||||||||||||
<input type="text" name="name'>
|
|||||||||||||||||||||||||||
<input type="submit" value="add">
|
|||||||||||||||||||||||||||
</form>
|
|||||||||||||||||||||||||||
echo $newname. ',';
|
|||||||||||||||||||||||||||
echo trim( $newname);
|
namenamename
|
||||||||||||||||||||||||||
it removes unnessceary space
|
|||||||||||||||||||||||||||
echo trim( $newname).',';
|
concatenate with comma , with comma it prints
|
||||||||||||||||||||||||||
vamsi,vamsi,
|
|||||||||||||||||||||||||||
echo trim( $newname).', ';
|
prints with space -- vamsi, vamsi, vamsi,
|
||||||||||||||||||||||||||
prints without last comma
|
|||||||||||||||||||||||||||
$readin = file('names.txt');
|
|||||||||||||||||||||||||||
$totalnames =
count($readin):
|
using count function, it reads how many lines
|
||||||||||||||||||||||||||
$c = 1;
|
c is initilised with 1
|
||||||||||||||||||||||||||
foreach ($readin as $newname){
|
for each is like loop, convert readin as newname
|
||||||||||||||||||||||||||
echo trim($newname);
|
ist cut the spaces either side of text in each line, then how
many commas print
|
||||||||||||||||||||||||||
if ($c < $totalnames){
|
ist print new line, compare the c with totla names, print comma
|
||||||||||||||||||||||||||
echo ', ';
|
incriment the c and go to print new line print comma c incrimets
toill c is less than totla
|
||||||||||||||||||||||||||
}
|
|||||||||||||||||||||||||||
$c++
|
|||||||||||||||||||||||||||
}
|
vamsi, vamsi, vamsi
|
||||||||||||||||||||||||||
we created line by line or array format
|
|||||||||||||||||||||||||||
and printed also
|
|||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||
| fgets | |||||||||||||||||||||||||||
| The PHP fgets() function is used to read single line from the file. | |||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||
this is first line
fgetc
The PHP fgetc() function is used to read single character from the file. To get all data using fgetc() function, use !feof() function inside the while loop.
- <?php
- $fp = fopen("c:\\file1.txt", "r");//open file in read mode
- while(!feof($fp)) {
- echo fgetc($fp);
- }
- fclose($fp);
The feof() function checks if the "end-of-file" (EOF) has been reached for an open file.
this is first line this is another line this is third linealready data is there in file, if we want to overwrite the data in file use write$fp = fopen('data.txt', 'w')if we want to append use a$fp = fopen('data.txt', 'a');/
how to delete the file
- <?php
- $status=unlink('data.txt');
- if($status){
- echo "File deleted successfully";
- }else{
- echo "Sorry!";
- }
- ?>
| fopen | ||||||||||||||||
| fopen | ||||||||||||||||
| fwrite | ||||||||||||||||
| fwrite | ||||||||||||||||
| fopen w | ||||||||||||||||
| fwrite | ||||||||||||||||
| fwrite | ||||||||||||||||
| fclose | ||||||||||||||||
| echo | ||||||||||||||||
| fopen a | ||||||||||||||||
| fwrite | ||||||||||||||||
| fclose | ||||||||||||||||
| echo | ||||||||||||||||
| echo | ||||||||||||||||
| post method | ||||||||||||||||
| echo | ||||||||||||||||
| file | ||||||||||||||||
| foreach | ||||||||||||||||
| echo , | ||||||||||||||||
| isset | ||||||||||||||||
| $_post | ||||||||||||||||
| !empty | ||||||||||||||||
| fopen | ||||||||||||||||
| fwrite | ||||||||||||||||
| fclose | ||||||||||||||||
| if else | ||||||||||||||||
| post method | ||||||||||||||||
| submit | ||||||||||||||||
| trim | ||||||||||||||||
| trim | ||||||||||||||||
| echo trim(variable). ',' | ||||||||||||||||
| file | ||||||||||||||||
| count | ||||||||||||||||
| foreach | ||||||||||||||||
| trim | ||||||||||||||||
| if | ||||||||||||||||
| c++ | ||||||||||||||||
|
No comments:
Post a Comment