Tuesday, 9 June 2020

php files create read open

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
$handle = fopen("c:\\folder\\file.txt""r");  
r Opens file in read-only mode. It places the file pointer at the beginning of the file.
r+ Opens file in read-write mode. It places the file pointer at the beginning of the file.
w Opens file in write-only mode. It places the file pointer to the beginning of the file and truncates the file to zero length. If file is not found, it creates a new file.
w+ Opens file in read-write mode. It places the file pointer to the beginning of the file and truncates the file to zero length. If file is not found, it creates a new file.
a Opens file in write-only mode. It places the file pointer to the end of the file. If file is not found, it creates a new file.
a+ Opens file in read-write mode. It places the file pointer to the end of the file. If file is not found, it creates a new file.
x Creates and opens file in write-only mode. It places the file pointer at the beginning of the file. If file is found, fopen() function returns FALSE.
x+ It is same as x but it creates and opens file in read-write mode.
c Opens file in write-only mode. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file
c+ It is same as c but it opens file in read-write mode.
fgets
The PHP fgets() function is used to read single line from the file.
  1. <?php    
  2. $fp = fopen("c:\\file1.txt""r");//open file in read mode    
  3. echo fgets($fp);  
  4. fclose($fp);  
  5. ?>    


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.

  1. <?php    
  2. $fp = fopen("c:\\file1.txt""r");//open file in read mode    
  3. while(!feof($fp)) {  
  4.   echo fgetc($fp);  
  5. }  
  6. 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 line


already 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

  1. <?php      
  2. $status=unlink('data.txt');    
  3. if($status){  
  4. echo "File deleted successfully";    
  5. }else{  
  6. echo "Sorry!";    
  7. }  
  8. ?>  
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++
how to create the file
how to write the data to created file
how to write the data to file
whats the keyword used to write into the file
how to write more than one word to a file varaible
how to open and close the text file and write the data into that file
how to open the file and write the file and close the file
how to append the entered text by user in a every new line
without entering they enterd submit what will happen
how to print the data in file at display
how to save the data to text file
how to print without space at either side
how to print that with comma and spoaceand trim at either
how to print with comma and no space
how to print the text in a file in with comma and without comma in last 



No comments:

Post a Comment