HomePHP File And Directory Manipulation In PHP (part 1)
File And Directory Manipulation In PHP (part 1)
PHP comes with a powerful and flexible file manipulation API that allows developers (among other things) to read and write files, view and modify file attributes, read and list directory contents, alter file permissions, and retrieve file contents into a variety of native data structures. Find out more, inside.
One of the most basic things you can do with any programming language is create and manipulate data structures. These structures may be transient - in-memory variables like arrays or objects - or more permanent - disk-based entities like files or database tables; however, the ability to create, modify and erase them in a convenient and consistent manner is of paramount importance to any programmer.
Like all widely-used programming languages, PHP has the very useful ability to read data from, and write data to, files on the system. But the language doesn't just stop there - PHP comes with a full-featured file-and-directory-manipulation API that allows you (among other things) to view and modify file attributes, read and list directory contents, alter file permissions, retrieve file contents into a variety of native data structures, and search for files based on specific patterns. This file manipulation API is both powerful and flexible - two characteristics that will endear it to any developer who's ever had to work with file manipulation commands.
Over the course of this tutorial, I'm going to introduce you to this filesystem API, demonstrating some of the common (and not-so-common) tasks you can perform with it. Note that this is not intended to be an exhaustive reference to all the file functions in PHP - there's a very good manual available for that - but rather an introduction to the functions you'll find yourself using most frequently, together with examples of how they can be used. If you've been using PHP for a while, some of this material will already be familiar to you; new users, on the other hand, should find most of it interesting.