Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Key Takeaways
Saves data to a file using PHP by writing and appending to a file
Full Transcript
hello and welcome to the 20th php basic tutorial uh this tutorial we're going to cover writing and appending to a file very similar write will just overwrite everything like it'll write every let's say you have a file that already exists and it has a few lines in it and use the right function it's just going to overwrite that whereas append will just kind of it will do that it will append to the file and won't delete what was maybe already there so if you were having like a log file you would want to use like append so anyway let's talk about that um so why don't we just go ahead and work that with the script here and we're gonna go ahead and get rid of this this way we just clean it up a little bit so we're not too confused but we'll use the submission data and store that data so now what we want to do is i change my mind let's we still want to echo that but then we just want to there and just because i'm a little tired let me make sure that works still we need to just like mess it up test me okay it does work that's good so we'll come back over here and now what we want to do is uh save the text like let's say if the text is entered so if not empty what do we want to do so let's make some space and we're going to say we're going to specify save file and that's going to be equal to use f open and then in here you there's two uh parameters the first parameter that you're going to specify is this name of the file if the file already exists great if it doesn't exist uh php will just make the file actually i suppose it's probably c that makes the file but php is going to tell c to go ahead and make the file so we're going to just call this file save.text and then in here you specify what you want to do are you going to read are you going to write or are you going to append in this case i think we should just depend or we'll go ahead and write um so then now that you've specified that if the file doesn't exist this line that we've written right now will create that file the next thing we want to do is let's go ahead and write to that file so let's do f write and again this will take two parameters the first one is going to be uh where are we writing to and then what shall we write and in this case let's go ahead and write hello and then it's always a good idea to go ahead and append a new line which is backslash n so full stop plus a backslash n colon and that's what we're going to write now as always with opening and writing files and such you always want to end with close otherwise if you come back to attempt to edit the file later it might restrict you it might say this is open you know elsewhere so let's save that and we'll upload this to our server yes and we'll go ahead and um oh we're going to need to specify what we want to close and in this case of course we want to close save files save that reupload so we'll go ahead and bring that up into our text editor and you can see it already uh saved hello um this is because uh we ran oops oh dear so as you can see we've already got um hello text is saved here upon the uploading of this php script and that is because um we actually have you know what it's saving the file is something we've you know predefined but what if we want to write to this file um how about we write text to that file so we'll save this we upload it and we'll pop over here and sample text misspelled luck balls save and now let's view edit this uh yes there we go and so you can see it's been rewritten to display sample text now like i was saying that's what write is going to do it's going to overwrite the stuff that's already there and put the new data in but what if we want to append it so um so the way that we can append data is instead of opening it with the intention to write let's open it with the intention to append otherwise everything stays the same you're still f writing you're still putting in the text you're still saving it so we'll save that throw it up to the server and this time let's write more sample tags and hopefully it will edit correctly you edit this card download a file a new and then here we see it's actually appended our sample text or smapple text um and then so on so you can continue um writing to this file so that's it for uh writing and appending to file as always thank you for watching and until next time
Original Description
Link to the full playlist:
http://www.youtube.com/playlist?list=PLQVvvaa0QuDcYpcjrNB43_iKqla5UeQw_
Sentdex.com
Facebook.com/sentdex
Twitter.com/sentdex
The basics of writing to a file in PHP
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 32 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
▶
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
Related AI Lessons
⚡
⚡
⚡
⚡
X now offers an MCP server to make its platform easier for AI tools to use
TechCrunch AI
n8n Automation Repurpose Video Content: The 2025 Production Guide
Dev.to AI
You’re Still Paying $200/Month for AI Tools You Could Replace With a Free Local Setup Tonight
Medium · Data Science
Top 10 AI Tools Every College Student Should Know in 2026
Medium · AI
🎓
Tutor Explanation
DeepCamp AI