Hudzilla.org - the homepage of Paul Hudson
Contents > Wish List | Report Bug | About Me ]

8     Files

This is NOT the latest copy of this book; click here for the latest version.

Once you master the art of working with files, a wider world of PHP web development opens up to you. Files aren't as flexible as databases by any means, but they do offer the chance to easily and permanently store information across scripts, which makes them popular amongst programmers.

Files, as you can imagine, can store all sorts of information. However, most file formats (e.g. picture formats such as PNG and JPEG) are binary, and very difficult and/or impossible to write using normal text techniques - in these situations you should use the library designed to cope with each format.

One reminder: if you are using an operating system that uses backslash \ as the path separator (e.g. Windows), you need to escape the backslash with another backslash, making \\. Owing to this, handling files can be quite different for Windows and Unix users - we will be covering both operating systems, naturally.

Topics covered in this chapter are:

  • Reading and writing files

  • Temporary files

  • How to make a counter

  • Handling file uploads

  • File permissions

Author's Note: CPUs work in billions of operations per second - a 3GHz CPU is capable of performing three billion operations every second. RAM access time is measured in nanoseconds (billionths of a second) - you can usually access some data in RAM in about 40 nanoseconds. Hard drive access time, however, is measured in milliseconds (thousandths of a second) - most hard drive have about a 7ms access time.

What this means is that hard drives are much, much slower than RAM, so working with files from your hard drive is the slowest part of your computer, excluding your CD ROM. Databases are able to store their data in RAM for much faster access time, whereas storing hard drive data in RAM is tricky.

Files are good for storing small bits of information, but it is not recommended that you use them too much for anything other than your PHP scripts themselves - counters are fine in files, as are other little things, but anything larger would almost certainly benefit from using a database. Having said that, please try to avoid the newbie mistake of putting everything into your database - if you find yourself trying to figure out what field type is right to store picture data, please have a rethink!


Chapter contents

8.1. Reading files
8.1.1. readfile()
8.1.2. file_get_contents() and file()
8.1.3. fopen() and fread()
8.2. Creating and changing files
8.2.1. file_put_contents()
8.2.2. fwrite()
8.3. Moving, copying, and deleting files: rename(), copy(), and unlink()
8.3.1. Moving files with rename()
8.3.2. Copying files with copy()
8.3.3. Deleting files with unlink()
8.4. Temporary files: tmpfile()
8.5. Other file functions: rewind(), and fseek()
8.6. Checking whether a file exists: file_exists()
8.7. Retrieving a file's status: is_readable(), is_writeable(), is_executable(), is_file(), is_dir(), and clearstatcache()
8.8. Dissecting filename information: pathinfo() and basename()
8.9. A working example: making a counter
8.10. Handling file uploads: move_uploaded_file()
8.10.1. Advanced file upload handling
8.10.2. Checking uploaded files: is_uploaded_file()
8.11. Locking files with flock()
8.12. Permissions
8.12.1. Setting permissions: chmod()
8.12.2. Changing file ownership: chown()
8.13. Working with directories: opendir(), readdir(), and closedir()
8.13.1. Deleting directories: rmdir()
8.13.2. One last directory function: scandir()
8.14. Remote files
8.15. File checksums: sha1_file() and md5_file()
8.16. Parsing a configuration file: parse_ini_file()
8.17. Summary
8.18. Exercises
8.19. Further reading
8.20. Next chapter



<< 7.12 Next chapter   8.1 Reading files >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Vijay L - 04 Jul 2008

I am a novice novice and you book is the best thing since sliced bread.God bless people like you.

.spider - 04 Jul 2008

hello. i'm currently reading your book, and it's remarkable. ^.^ i expect to learn much from it.

A PHP User - 04 Jul 2008

hi there

cloud1068@gmail.com - 04 Jul 2008

I want to let you know, That I think your book is very good! It's free and has everything I could want to know about PHP, thank you for going through all the work and still letting your book be free online.

A PHP User - 04 Jul 2008

I dont know who this person is.... but i love your tutorials they help me so much.. i cant wait to get rich off you book

Bored - 04 Jul 2008

I guess i have found two people like myself below

Bored - 04 Jul 2008

I guess i have found two people like myself below

Bored - 04 Jul 2008

I guess i have found two people like myself below

Bored - 04 Jul 2008

I guess i have found two people like myself below

A cracker - 04 Jul 2008

Hello, A hacker below
I am a Craker, Nice to meet you here.

A hacker - 04 Jul 2008

Hello



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is seven plus four?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow