From the course: Learning PHP

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Writing files

Writing files - PHP Tutorial

From the course: Learning PHP

Start my 1-month free trial

Writing files

- [Instructor] Along with reading from files, you can write to files. In this case, you can only write to files you have permission to write to. So this is great if you want to store information, but don't necessarily have access to a database. On the outset here, I'll say that depending on where you live, there may be different laws governing how you store data. If you're going to store user data, make sure you comply with the appropriate laws. Okay, with that disclaimer out of the way, let's take a look at how to write to files. The simplest way would be to use the file put content function, which will open a file, dump the text and return the number of bytes written. So let's say we want to make a copy of our "Treasure Island" file. We could say text gets file get contents. And then the treasure island file. And then we can say echo. So we have something to look at. It'll be the number of bytes. File put contents.…

Contents