From the course: PHP Techniques: Working with Files and Directories
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Writing to files - PHP Tutorial
From the course: PHP Techniques: Working with Files and Directories
Writing to files
- [Instructor] In this movie we'll learn how to write data to files using PHP. We will learn two techniques. The first, is to use the PHP function fwrite. We learned fread earlier, and this is its counterpart. The first argument is a handle for an open file, we must have a file open for writing first. The second argument is the data that we want to write. It will add that string to the open file, and return the number of bytes that were written, or false if there was an error. You can capture that return value in a variable, but most of the time you don't need to. If you call fwrite more than once, it adds the new data after the previously written data. The second technique is to use the function, file_put_contents. It's similar to file_get_contents that we saw earlier. It's also a shortcut which opens a file, writes the data and closes the file all in one step. It's useful if you just want to write data to a file all at…
Contents
-
-
-
(Locked)
Filesystem basics6m 6s
-
Relative paths and magic constants4m 53s
-
(Locked)
Accessing files5m 4s
-
(Locked)
Reading from files4m 52s
-
(Locked)
Reading line by line5m 9s
-
(Locked)
File permissions5m 34s
-
(Locked)
Writing to files5m 5s
-
(Locked)
Writing line returns2m 28s
-
(Locked)
Deleting files2m 38s
-
(Locked)
Moving the file pointer7m 36s
-
(Locked)
Examining file details6m 52s
-
(Locked)
Challenge: Create a log file2m 2s
-
(Locked)
Solution: Create a log file3m 17s
-
(Locked)
-
-
-