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

Writing to files - PHP Tutorial

From the course: PHP Techniques: Working with Files and Directories

Start my 1-month free trial

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