Writing and Appending to Files in PHP in Hindi | PHP Tutorial #37

CodeWithHarry
10 May 202011:38

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

00:00

πŸ“‚ 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.

05:02

✏️ 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.

10:03

πŸ“œ 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

PHP, which stands for Hypertext Preprocessor, is a widely-used open-source server-side scripting language that is especially suited for web development. In the video, PHP is the central theme as the script discusses how to handle files using PHP functions. It is exemplified when the speaker creates a '37files.php' file to demonstrate file operations.

πŸ’‘Apache server

The Apache server is a widely used web server software that powers many websites on the internet. In the context of the video, the speaker mentions starting the Apache server to facilitate the execution of PHP scripts, indicating the server's role in serving web pages that include PHP code.

πŸ’‘File pointer

A file pointer is a reference to a specific location in a file. In the video, the concept of a file pointer is likened to obtaining a 'token' at a mall, which is necessary to perform operations like reading or writing to a file in PHP. The file pointer is essential for file handling operations such as opening, reading, writing, and closing files.

πŸ’‘Write mode

Write mode in PHP is a file opening mode used with the fopen() function, denoted by 'w'. It is explained in the video that when a file is opened in write mode, if it already exists, its contents are truncated (emptied), and if it does not exist, a new file is created. The video demonstrates how fwrite() is used in conjunction with write mode to overwrite the contents of 'myfile2.txt'.

πŸ’‘Append mode

Append mode in PHP, indicated by 'a', is used to open a file for writing only at the end of the file without truncating it. The video script explains that when using append mode, new content is added to the end of the file without affecting the existing content, which is useful for creating log files where data is sequentially added.

πŸ’‘fopen

fopen() is a PHP function used to open a file or a URL. In the video, the speaker uses fopen() to create a file pointer for file operations. The function is essential for file handling as it initiates the process of reading from or writing to a file, with the mode specified by the user (e.g., 'w' for write or 'a' for append).

πŸ’‘fwrite

fwrite() is a PHP function for writing data to a file. The video demonstrates fwrite() being used to add content to a file that has been opened in write or append mode. The function is showcased as a way to repeatedly write new lines to a file, overwriting in write mode or appending in append mode.

πŸ’‘fclose

fclose() is a PHP function used to close an open file pointer. Although the video mentions that PHP automatically closes files at the end of the script execution, it emphasizes the good practice of using fclose() to ensure files are properly closed after their operations are completed, which is important for maintainability.

πŸ’‘Overwrite

Overwriting in the context of file handling refers to the process of replacing the existing content of a file with new content. The video explains that when a file is opened in write mode, its existing content is lost, and fwrite() will start writing from the beginning of the file, effectively overwriting the old data.

πŸ’‘File handling

File handling in PHP encompasses a set of operations performed on files, such as opening, reading, writing, and closing. The video script is focused on demonstrating file handling techniques in PHP, specifically how to write to files using different modes and the implications of each operation on the file's content.

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

play00:00

Alright guys, so we have seen a lot of things.

play00:02

We have seen the files, how to handle them in PHP.

play00:06

I am starting my Apache server.

play00:09

You can also start your Apache server here if you have not done it yet.

play00:13

And here what I will do is open my PHP tutorial playlist.

play00:16

Along with that, we come here and make a new file whose name we are going to keep.

play00:22

Santisfiles.php

play00:26

And here PHP is which we are going to name as 37files.php and here

play00:28

php is

play00:32

you can write it like this

play00:34

and now

play00:36

we are going to see how to write

play00:38

the files in php, so this file

play00:40

which I have made, 37files.php

play00:42

you have to

play00:44

access it from here

play00:46

so this index file which I have bookmarked

play00:49

you will get it here

play00:52

open it, there is nothing in this file, if you want you can write welcome

play00:55

to see if it is working or not

play01:02

write files in php

play01:04

oops, I have commented it by mistake

play01:08

if i do it like this then echo has been created

play01:12

now how to write files here

play01:15

first of all how to read files

play01:17

to read myfile.txt

play01:20

i have made my file pointer

play01:22

and it has to be made here

play01:24

it is said that

play01:26

suppose there is a big mall

play01:28

I am giving a good analogy

play01:30

it came in my mind

play01:32

there is a big mall, you get food there

play01:34

you get to watch movie there

play01:36

so what you have to do first

play01:38

in most of the cases, you have to

play01:40

take the token first

play01:42

the token is available there

play01:44

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

play01:45

there is a token for food also, if you want to buy something

play01:48

there is a token for that also, so take a token first

play01:51

if you want to eat dosa, maggi, chowmein, whatever you want to eat

play01:54

bring a token from the counter first

play01:57

then he will give you the token, so this token

play02:00

is like a file pointer, so

play02:03

here I have copied my file pointer so here I have copied my file pointer

play02:06

and here I

play02:08

will make a file named myfile2

play02:11

because myfile is this and here

play02:14

I will write the mode, I will make it w

play02:17

and once I have made the mode w

play02:20

here I will show you one thing

play02:23

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.

play02:26

Sorry, this is not a python program.

play02:29

I have to run it here. See, as soon as I tried to open it,

play02:32

it was automatically created.

play02:35

It was not there. It was not there. It was automatically created.

play02:38

As soon as you open any file in PHP

play02:41

in write mode,

play02:44

it will automatically be yours. you will open any file in PHP in write mode

play02:45

it will automatically become yours

play02:48

it will never be like myfile3

play02:51

and if I run it

play02:54

then you can see myfile3 is also created

play02:57

if you make myfile10, it will become 10x10

play03:00

the whole folder will be filled if you keep on

play03:03

reloading it with different file names

play03:06

now here we have myfile3.txt

play03:08

let me do one thing

play03:10

let me come back to myfile2

play03:12

and let me rename myfile3 to delete

play03:14

not rename delete

play03:16

I have deleted this file

play03:18

now what I want to do

play03:20

I want to write

play03:22

and as F read, F write

play03:24

it is a very nice

play03:26

and beautiful syntax of PHP, I call it a very beautiful language

play03:29

so you use F write and

play03:32

give the first argument, file pointer to it

play03:35

and what will you give the second argument to it

play03:38

give the second argument to it, whatever you want to write in it

play03:41

so I want to write

play03:44

this is the best file on this planet

play03:46

please don't argue with me on

play03:52

this one.

play03:55

Ok.

play03:55

So I will save it

play03:57

if I put semicolon here

play03:58

now this is only one line

play03:59

I have turned on word wrap here

play04:01

if you will turn off word wrap

play04:03

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

play04:05

you can do that too

play04:08

but while making tutorial

play04:10

I will keep it on

play04:14

I have reloaded it

play04:17

and now my file

play04:20

which was 2.txt

play04:21

has content in it

play04:24

so let's say I wrote this content

play04:26

now if I reload it 10 times

play04:29

I reloaded it 10 times

play04:32

I reloaded it 10 times

play04:35

and I want you to guess in the comment section

play04:38

I don't want you to know it beforehand

play04:41

I just want you to guess what will happen

play04:44

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

play04:46

I will show you what will happen

play04:48

so here

play04:50

what will happen is

play04:52

this write

play04:54

will write this line only once

play04:56

and you will think why it write once

play04:58

I will tell you the funda

play05:00

why it write once

play05:02

and I will close all these files

play05:04

so I will close others first and then open myfile2.txt

play05:09

so what happens when you open any file in write mode

play05:15

then if it doesn't exist then it will be made

play05:20

if it exists then whatever content you put in it will be overwrite

play05:26

literally overwrite, you will lose all that content

play05:28

ok

play05:30

so here 37 files.php

play05:32

I will run

play05:34

this file once

play05:36

so what will happen, this content will come in my file 2

play05:38

if I

play05:40

write here again

play05:42

this is

play05:44

another content

play05:46

and again you have to close the file

play05:49

you have to do it, you have to do it, you have to do it

play05:52

and anyway, I sometimes

play05:55

run it like python or notejs

play05:57

but here it is saying close, ok I have to give the file pointer

play06:00

also

play06:02

it has to run with magic, it will know which file to close

play06:04

maybe I have opened 10 files are open

play06:06

now if I replicate it

play06:08

I will show you a very interesting thing

play06:10

and here I will write

play06:12

this is another content

play06:14

so what happens is

play06:16

once you open the file

play06:18

then if it is present

play06:20

then it becomes empty in write mode

play06:22

and after that if you run fwrite

play06:24

10 times then it will be empty in write mode and if you run fwrite 10 times

play06:26

then it will write it again and again

play06:29

in the file, like if I show you

play06:32

and now I show you the file

play06:35

so see it has put all the things in it

play06:38

it has put this is another content also

play06:41

so what will happen if I comment out both of them

play06:44

and now run this file

play06:46

so see this file is empty

play06:48

means when you will open it in write mode

play06:52

at that moment it will be empty

play06:56

if it exists then it will be

play06:58

and after that whatever fwrite you will run

play07:02

the content will come coming, coming, coming

play07:06

so if I put a third

play07:08

fptr, this is

play07:10

another content 3 and I

play07:12

backslash n here so that

play07:14

clarity will be made, what are we doing

play07:16

what content

play07:18

are we writing, reload it

play07:20

in my file 2

play07:22

the more I fwrite it

play07:24

what will happen

play07:26

you can see

play07:28

it will keep happening

play07:30

we will keep writing in this

play07:32

so what I did

play07:34

as soon as I opened a file in write mode

play07:37

what I got

play07:39

it will make the file empty

play07:41

if it exists and if it doesn't exist

play07:43

then it will be created after that, how many times I will write

play07:46

fwrite, till then this content

play07:48

will keep writing in the file, so this is the writing

play07:50

to a file, okay, so

play07:52

I will do one thing

play07:54

I will write it here

play08:00

writing to a file in php

play08:02

I will write

play08:04

and now what I will do here

play08:06

it will close here

play08:09

now I will write here

play08:10

appending to a file in PHP

play08:13

now see we have made a very nice file

play08:15

it has so much content

play08:16

suppose you want to make a log file type

play08:21

where you want to do something like

play08:23

that your file

play08:26

content will be added at the end of it

play08:29

whatever you want to do

play08:31

so in this what you will do

play08:33

you will open it in append mode

play08:35

and for append you have to write a

play08:37

a means append

play08:40

your file will open in append mode

play08:43

I saved it

play08:46

and see what is the content in this file

play08:49

and in append mode

play08:52

if I run fwrite

play08:54

then what will happen, see for yourself

play08:57

at the end of this

play08:59

this is being

play09:02

appended to the file

play09:04

and by the way, there don't need to use fclose

play09:08

because when your php script is finished, it will work

play09:12

but it's a good practice to use fclose

play09:16

so that if you want to do something later, your file is closed here

play09:20

so it's very good to use fclose from the maintainability perspective

play09:24

I will run it once and see what is the content of the file

play09:26

this is being appended to the file

play09:28

it is written here

play09:30

I will run it once more

play09:32

see this is being appended to the file

play09:34

it is again written here

play09:36

I will run it once more

play09:38

see this is being appended to a file

play09:40

so what is the meaning of append

play09:42

it means to add it at the end

play09:44

so by appending it means to connect at the end.

play09:46

So by appending it will connect at the end.

play09:48

So I hope you understood how it connects at the end.

play09:51

But in write mode

play09:54

remember that your file gets overwritten.

play09:57

So whatever data is there

play10:00

you lose it. So in write mode

play10:03

it overwritesrites you should understand this

play10:08

I hope you understood this

play10:10

alright guys, if you haven't accessed my

play10:12

php playlist yet, kindly do it

play10:16

and if you will watch this playlist from the start

play10:20

then you will benefit

play10:22

it will take some time because I have started it from basic

play10:26

even I have told you the basics of web development 101

play10:31

so it will take some time to complete it

play10:35

but if you complete it

play10:37

then I will give you confidence through projects and exercises

play10:43

that you will never get stuck in PHP

play10:46

in your life. So guys

play10:48

this was our PHP file reading

play10:50

and writing. You must have understood

play10:52

the difference between write mode and append mode

play10:54

you will get all the source code

play10:56

but still

play10:58

I request you

play11:00

to type it yourself. If you type

play11:02

you will learn a lot.

play11:04

So guys if if you haven't

play11:06

liked this video yet, then do like it.

play11:09

And click on the playlist above and click on the bookmark

play11:12

and click below to save it. That's all for this video.

play11:15

Thank you so much guys for watching this video.

play11:18

And I will see you next time

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
PHP BasicsFile HandlingWeb DevelopmentCoding TutorialApache ServerProgramming TipsBeginner GuideFile OperationsPHP ScriptingTech Learning