Writing and Appending to Files in PHP in Hindi | PHP Tutorial #37
Summary
TLDRIn this informative tutorial, the presenter demonstrates the basics of file handling in PHP, including opening, writing, and appending to files. They introduce the concept of file pointers, akin to 'tokens' in a mall, essential for accessing and manipulating files. The video covers creating files in write mode, which overwrites existing content, and append mode, which adds data at the end. The presenter emphasizes the importance of closing files after use and encourages viewers to follow along with their PHP tutorial playlist for a comprehensive understanding of PHP file operations.
Takeaways
- 😀 The video discusses handling files in PHP, including creating and writing to them.
- 🔑 It emphasizes the importance of starting the Apache server for PHP file testing.
- 📝 The script demonstrates creating a new PHP file named '37files.php' to illustrate file operations.
- 📑 It explains how to write to a file in PHP using 'fopen' for opening a file and 'fwrite' for writing content.
- 🛒 The analogy of a 'token' in a mall is used to explain the concept of a file pointer in PHP.
- 📂 The video clarifies that opening a file in 'w' (write) mode will create the file if it doesn't exist or overwrite it if it does.
- 🔄 It shows that running 'fwrite' multiple times on an opened file in write mode will repeatedly overwrite the content.
- 📝 The script also covers appending to a file in PHP using 'a' mode, which adds content to the end of the file without overwriting existing data.
- 🔒 The importance of closing files with 'fclose' after operations is highlighted for good practice, even though PHP scripts close files automatically.
- 📚 The presenter encourages viewers to watch the PHP playlist from the start for a comprehensive understanding of PHP and web development basics.
- 👍 The video ends with an invitation for viewers to like the video, bookmark the playlist, and engage with the content.
Q & A
What is the purpose of the video script?
-The purpose of the video script is to teach viewers how to handle file operations in PHP, specifically how to write to files using different modes.
How does the script suggest starting the Apache server for PHP file operations?
-The script suggests starting the Apache server by accessing the PHP tutorial playlist and following along with the instructions provided there.
What is the analogy used in the script to explain the concept of a file pointer?
-The script uses the analogy of a big mall where one needs to get a 'token' to access different services, similar to how a file pointer is used to access and manipulate files in PHP.
What is the significance of the 'w' mode in PHP file operations?
-The 'w' mode in PHP file operations is significant because it opens a file for writing, and if the file does not exist, it is created. If it does exist, the content is overwritten.
How does the script demonstrate the creation of a new file in PHP?
-The script demonstrates the creation of a new file by opening a file in 'w' mode, which automatically creates the file if it does not already exist.
What is the difference between writing to a file in 'w' mode and appending to a file in 'a' mode?
-Writing to a file in 'w' mode overwrites any existing content in the file, while appending to a file in 'a' mode adds new content to the end of the existing file without erasing what's already there.
Why is it important to close a file after writing to it in PHP?
-It is important to close a file after writing to it in PHP to ensure that the changes are saved and to free up system resources. It also prevents potential data corruption or loss.
What does the script suggest for maintaining good practice when working with files in PHP?
-The script suggests using the 'fclose' function to close the file after writing or appending to it, as it is a good practice for maintainability and ensures that the file is properly closed after its operations are completed.
How does the script encourage viewers to learn PHP file operations effectively?
-The script encourages viewers to learn by typing the code themselves, as it helps in better understanding and retention of the concepts.
What is the script's final recommendation for viewers who want to learn PHP thoroughly?
-The script's final recommendation is for viewers to watch the PHP playlist from the start, as it covers the basics and progresses to more advanced topics, providing confidence through projects and exercises.
Outlines
📂 Introduction to Handling Files in PHP
In this segment, the instructor explains how to start an Apache server and introduces the process of creating and handling files in PHP. They create a new PHP file named '37files.php' and discuss reading and writing files. The analogy of a token system in a mall is used to explain file pointers. The instructor demonstrates how opening a file in write mode in PHP automatically creates it if it doesn't exist and highlights the functionality of 'fwrite' for writing to files.
✏️ Writing to a File in PHP
This paragraph continues the discussion on writing to files in PHP. The instructor explains that when a file is opened in write mode, its existing content is overwritten. They show how to use 'fwrite' to write content to a file multiple times and illustrate this by writing different content into 'myfile2.txt'. The process of closing files and the importance of using 'fclose' for maintainability are also covered.
📜 Appending to a File in PHP
The focus shifts to appending content to a file in PHP. The instructor demonstrates how to open a file in append mode using 'a' and explains that this mode adds content to the end of the file rather than overwriting it. The practical use of appending for creating log files is mentioned. The segment concludes with a comparison between write mode and append mode, emphasizing the overwrite nature of write mode.
📚 Conclusion and Further Learning
In the final paragraph, the instructor encourages viewers to access the PHP tutorial playlist from the beginning to gain a comprehensive understanding of PHP and web development basics. They stress the importance of practicing by typing out the code to solidify learning. The video wraps up with a call to action to like the video and bookmark the playlist for future reference. The instructor expresses gratitude to the viewers and signs off.
Mindmap
Keywords
💡PHP
💡Apache server
💡File pointer
💡Write mode
💡Append mode
💡fopen
💡fwrite
💡fclose
💡Overwrite
💡File handling
Highlights
Introduction to handling files in PHP and starting the Apache server.
Creating a new PHP file named '37files.php' for the tutorial.
Explanation of PHP syntax for writing to files.
Analogy of a file pointer to a token in a mall for accessing services.
Demonstration of file creation in write mode with 'myfile2'.
Explanation of how write mode in PHP overwrites existing files.
Using 'fopen', 'fwrite', and 'fclose' functions for file operations.
Illustration of file content overwriting with multiple 'fwrite' calls.
Clarification on the effect of write mode resetting file content to empty.
Introduction to 'append' mode for adding content to the end of a file.
Demonstration of appending text to an existing file without overwriting.
Emphasis on the importance of closing files with 'fclose' for maintainability.
Highlighting the difference between write mode and append mode in PHP file handling.
Encouragement to watch the PHP playlist from the start for a comprehensive understanding.
Offer of confidence building through projects and exercises in the PHP playlist.
Request for viewers to type the code themselves to enhance learning.
Invitation to like the video and follow the playlist for more tutorials.
Transcripts
Alright guys, so we have seen a lot of things.
We have seen the files, how to handle them in PHP.
I am starting my Apache server.
You can also start your Apache server here if you have not done it yet.
And here what I will do is open my PHP tutorial playlist.
Along with that, we come here and make a new file whose name we are going to keep.
Santisfiles.php
And here PHP is which we are going to name as 37files.php and here
php is
you can write it like this
and now
we are going to see how to write
the files in php, so this file
which I have made, 37files.php
you have to
access it from here
so this index file which I have bookmarked
you will get it here
open it, there is nothing in this file, if you want you can write welcome
to see if it is working or not
write files in php
oops, I have commented it by mistake
if i do it like this then echo has been created
now how to write files here
first of all how to read files
to read myfile.txt
i have made my file pointer
and it has to be made here
it is said that
suppose there is a big mall
I am giving a good analogy
it came in my mind
there is a big mall, you get food there
you get to watch movie there
so what you have to do first
in most of the cases, you have to
take the token first
the token is available there
movie ticket is there, food token is there if you have to take a token, that you are getting a token there for which you have to take a movie ticket
there is a token for food also, if you want to buy something
there is a token for that also, so take a token first
if you want to eat dosa, maggi, chowmein, whatever you want to eat
bring a token from the counter first
then he will give you the token, so this token
is like a file pointer, so
here I have copied my file pointer so here I have copied my file pointer
and here I
will make a file named myfile2
because myfile is this and here
I will write the mode, I will make it w
and once I have made the mode w
here I will show you one thing
oops I don't have to run this I have to run here. Let me show you something here. Oops, I don't have to run it. I have to run it from here.
Sorry, this is not a python program.
I have to run it here. See, as soon as I tried to open it,
it was automatically created.
It was not there. It was not there. It was automatically created.
As soon as you open any file in PHP
in write mode,
it will automatically be yours. you will open any file in PHP in write mode
it will automatically become yours
it will never be like myfile3
and if I run it
then you can see myfile3 is also created
if you make myfile10, it will become 10x10
the whole folder will be filled if you keep on
reloading it with different file names
now here we have myfile3.txt
let me do one thing
let me come back to myfile2
and let me rename myfile3 to delete
not rename delete
I have deleted this file
now what I want to do
I want to write
and as F read, F write
it is a very nice
and beautiful syntax of PHP, I call it a very beautiful language
so you use F write and
give the first argument, file pointer to it
and what will you give the second argument to it
give the second argument to it, whatever you want to write in it
so I want to write
this is the best file on this planet
please don't argue with me on
this one.
Ok.
So I will save it
if I put semicolon here
now this is only one line
I have turned on word wrap here
if you will turn off word wrap
then this scroll bar will come out so you can do that too but I will make tutorial on word wrap if you turn off word wrap, you will get a scroll bar
you can do that too
but while making tutorial
I will keep it on
I have reloaded it
and now my file
which was 2.txt
has content in it
so let's say I wrote this content
now if I reload it 10 times
I reloaded it 10 times
I reloaded it 10 times
and I want you to guess in the comment section
I don't want you to know it beforehand
I just want you to guess what will happen
if you tell me in the comment section I will show you the initial guess what will happen if you tell me in the comment
I will show you what will happen
so here
what will happen is
this write
will write this line only once
and you will think why it write once
I will tell you the funda
why it write once
and I will close all these files
so I will close others first and then open myfile2.txt
so what happens when you open any file in write mode
then if it doesn't exist then it will be made
if it exists then whatever content you put in it will be overwrite
literally overwrite, you will lose all that content
ok
so here 37 files.php
I will run
this file once
so what will happen, this content will come in my file 2
if I
write here again
this is
another content
and again you have to close the file
you have to do it, you have to do it, you have to do it
and anyway, I sometimes
run it like python or notejs
but here it is saying close, ok I have to give the file pointer
also
it has to run with magic, it will know which file to close
maybe I have opened 10 files are open
now if I replicate it
I will show you a very interesting thing
and here I will write
this is another content
so what happens is
once you open the file
then if it is present
then it becomes empty in write mode
and after that if you run fwrite
10 times then it will be empty in write mode and if you run fwrite 10 times
then it will write it again and again
in the file, like if I show you
and now I show you the file
so see it has put all the things in it
it has put this is another content also
so what will happen if I comment out both of them
and now run this file
so see this file is empty
means when you will open it in write mode
at that moment it will be empty
if it exists then it will be
and after that whatever fwrite you will run
the content will come coming, coming, coming
so if I put a third
fptr, this is
another content 3 and I
backslash n here so that
clarity will be made, what are we doing
what content
are we writing, reload it
in my file 2
the more I fwrite it
what will happen
you can see
it will keep happening
we will keep writing in this
so what I did
as soon as I opened a file in write mode
what I got
it will make the file empty
if it exists and if it doesn't exist
then it will be created after that, how many times I will write
fwrite, till then this content
will keep writing in the file, so this is the writing
to a file, okay, so
I will do one thing
I will write it here
writing to a file in php
I will write
and now what I will do here
it will close here
now I will write here
appending to a file in PHP
now see we have made a very nice file
it has so much content
suppose you want to make a log file type
where you want to do something like
that your file
content will be added at the end of it
whatever you want to do
so in this what you will do
you will open it in append mode
and for append you have to write a
a means append
your file will open in append mode
I saved it
and see what is the content in this file
and in append mode
if I run fwrite
then what will happen, see for yourself
at the end of this
this is being
appended to the file
and by the way, there don't need to use fclose
because when your php script is finished, it will work
but it's a good practice to use fclose
so that if you want to do something later, your file is closed here
so it's very good to use fclose from the maintainability perspective
I will run it once and see what is the content of the file
this is being appended to the file
it is written here
I will run it once more
see this is being appended to the file
it is again written here
I will run it once more
see this is being appended to a file
so what is the meaning of append
it means to add it at the end
so by appending it means to connect at the end.
So by appending it will connect at the end.
So I hope you understood how it connects at the end.
But in write mode
remember that your file gets overwritten.
So whatever data is there
you lose it. So in write mode
it overwritesrites you should understand this
I hope you understood this
alright guys, if you haven't accessed my
php playlist yet, kindly do it
and if you will watch this playlist from the start
then you will benefit
it will take some time because I have started it from basic
even I have told you the basics of web development 101
so it will take some time to complete it
but if you complete it
then I will give you confidence through projects and exercises
that you will never get stuck in PHP
in your life. So guys
this was our PHP file reading
and writing. You must have understood
the difference between write mode and append mode
you will get all the source code
but still
I request you
to type it yourself. If you type
you will learn a lot.
So guys if if you haven't
liked this video yet, then do like it.
And click on the playlist above and click on the bookmark
and click below to save it. That's all for this video.
Thank you so much guys for watching this video.
And I will see you next time
Ver Más Videos Relacionados
5.0 / 5 (0 votes)