PHP Tutorial (& MySQL) #40 - File System (part 2)

Net Ninja · Beginner ·🔧 Backend Engineering ·7y ago

Key Takeaways

This PHP tutorial covers file system operations, including opening, reading, writing, and deleting files, using functions like fopen, fread, fgets, fgetc, and fclose, with examples and explanations of different file modes and error handling.

Full Transcript

all right then gang so we've already seen how to quickly read the file using the read file function we did that in the last video but now I want to show you how we can actually open a file and keep a reference to that open file in a variable and this way will'll offer us more options than the read file method and once we have that file open we can do multiple things with it like read it and write to it so first of all we need to actually open the file so at the minute I've got this file and quotes. txt that's what we're going to be reading and opening and I've stored that read me. text in fact this needs to be quotes. text inside this variable so now we want to open this file to read so to do that we're going to store the reference to the open file in a handle and we're going to be using that handle to do things in the future so set it equal to F open that's the function name to open the file then we pass in as the first argument the file we want to open which is quotes. txt and then the second argument is going to be a little code and we're going to use R now there's many different codes and we'll go through a few of them later on but this basically means that we want to open it up for reading only so when we open this using this code and store it in this handle we can only read from this file we can't write to it at the minute okay so then how do we read from the file well there's various different ways the first way I'm going to show you is to use the F read function so we're going to Echo this out F read and then we pass in the handle this time notice we pass in the handle and not the file itself we've opened that file and we're storing the reference to that open file in a handle now and now we're using this handle to read the file so that's the first argument the second argument is how many bytes we want to read of this file well I want to read the whole file size and I'm not too sure what that size is so I could use the file size meth meth that we saw in the last video to use to find out the size of this file in bytes and pass that in here so we'll say file size pass in the file and then that should do so that is going to read all of the file and Echo it to the screen so if we refresh we can see all of that file right here okay so if we wanted to just read maybe 112 characters or 112 bytes rather we could if we wanted to we could say F read and then we want to pass in the handle and also the btes 112 okay so let me comment out the first one up here and save this refresh over here and we can see just this bit now the first 112 bytes of the file cool so that's one way we can read the file now another way is by reading a single line of the file and we do that using a different function called f gets or F get S so we can say Echo F gets s for single line and then pass in the handle now this time we don't need to pass in a second argument the number of bytes because we're just reading a single line no matter how long or short that is so if we save now and refresh we should see that first line now notice what happens if I do this again I'm going to duplicate this onto the next line and we're doing it twice now if I refresh then we don't get the same line twice we get the first line then the next line so what's going on there well this is because of a thing called a pointer when it comes to reading files so you can think of a pointer as a bit like your cursor when you open the file that cursor is at the start of the file and so is the pointer now when you read a line the pointer is moved to the end of that line now when you read another line it reads a line from the position of the pointer at that moment in time so the pointer is here because we've previously read a line and reads the next line and then the pointer would be here at the end of that line if we read another line it would start from here and read this line and the point would go to there Etc okay so that's this point in action now we can read single lines but we can also read single characters so to do that we use another function and that is called f get C so this is f get S for single and this is going to be F get C for character so I'm going to to pass in the handle again and if I save this and refresh over here then we can see T now that is the next character over here T because currently the point is there because remember we've just read two lines so the pointer is still here and we're reading the next character now if we were to comment these out then it would start at the very beginning because we've not read the lines now and so the first character would be a b now if we do it twice we should get the word b so let me save that and refresh over here and we see B awesome all right then so that's how we can read from the file now what if we wanted to write to the file well currently like I said we couldn't do that because we've opened the file with this code R which means read only if we wanted to write to the file we'd have to use a different code here now one of the options is using r+ and that allows us to read and write to the file now if we wanted to write to the file we'd use a function called f write and the first argument is going to be the handle the second argument is what we want to write to the file so I'm going to use quotes here and I'm going to say new line first of all so back sln that escapes the character and goes to a new line then I'm going to say everything popular is wrong now what's going to happen here where's it going to write it to in the file well if I save this in fact I'm going to copy all of this in case I need to paste it back in later notice the file it starts with this quote and ends with this quote right now if I save this and refresh in the browser it's going to run that file and if we go to quotes now we see this is the first quote and the first quote previously has gone right so we said write this new line and it's done that it's gone to a new line so one is empty and then this quote and it's written that right here everything popular is wrong but then we get the remainder of the other quote as well okay so it's written it at the start of the file and it's written over the stuff that was already there if we paste in the original we can see this was the first quote and it's just written over this up until up here somewhere all right so what if we didn't want to do that what if we wanted to write from the end well instead what we could do is use a character a or A+ and what that does is when we open it it places the pointer at the end over here okay so this is back to the original and we're opening it up now using this thing right here which allows us to write from the end because it places the point at the end so now when we write this we should see the quote added on so if we save it and run it again by clicking refresh go back to quotes and we see everything popular is wrong at the very end now okay so that's how we write to a file as well now now there's loads of different codes we can use well not loads but a handful I'm not going to show you them all but what I will do is leave a link to this list right here which says what each one is for okay now it's always good practice when we open a file and do something with that file to later on close the file and we do that by using F close and passing in the F uh the handle rather that we want to close so this thing over here okay now I want to show you one more thing and that is how to delete a file and with do that by using the function unlink so we passing the file this time not the handle the file that we want to delete which is this thing quotes. txt we store that up here and that is going to delete it for us when we run this file refresh and boom it goes cool so there we go my friends that is a better way to open a file and read it and also write to the file as well

Original Description

Extra reading - https://www.w3schools.com/php/func_filesystem_fopen.asp ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: + Course files - https://github.com/iamshaunjp/php-mysql-tutorial + VS Code editor - https://code.visualstudio.com/ + Materialize Playlist - https://www.youtube.com/watch?v=gCZ3y6mQpW0&list=PL4cUxeGkcC9gGrbtvASEZSlFEYBnPkmff 🤑🤑 Donate + https://www.paypal.me/thenetninja 🎓🎓 Find me on Udemy + https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This tutorial teaches how to perform file system operations in PHP, including opening, reading, writing, and deleting files, with examples and explanations of different file modes and error handling.

Key Takeaways
  1. Open a file using fopen
  2. Read from a file using fread, fgets, or fgetc
  3. Write to a file using fwrite
  4. Close a file using fclose
  5. Delete a file using unlink
💡 Understanding the different file modes and functions in PHP is crucial for performing file system operations effectively.

Related Reads

📰
11 Tiny Bash Scripts That Save Me Time Every Day
Learn 11 tiny Bash scripts to automate daily tasks and boost productivity
Medium · AI
📰
11 Tiny Bash Scripts That Save Me Time Every Day
Learn 11 tiny Bash scripts to automate daily tasks and boost productivity
Medium · Programming
📰
I Thought My Senior Backend Interview Answers Needed More Detail. Then I Started Timing Them.
Improve your senior backend interview answers by timing them to ensure conciseness and effectiveness
Medium · Programming
📰
I Failed Several Senior Backend Interviews After Giving Answers I Thought Were Correct.
Learn from a candidate's experience of failing senior backend interviews despite giving answers they thought were correct, and discover key areas for improvement in databases, queues, caching, and production incidents.
Medium · Programming
Up next
Unlock CRAZY Performance: Native Code Compilation No JNI! #shorts #quarkusinsights #projectpanama
Quarkusio
Watch →