Practical PHP Programming:Working with files
From IpbWiki
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
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
- 7.1. Practical_PHP_Programming:Reading files
- 7.2. Practical_PHP_Programming:Creating and changing files
- 7.3. Practical_PHP_Programming:Moving files
- 7.4. Practical_PHP_Programming:Copying files
- 7.5 Practical_PHP_Programming:Deleting files
- 7.6. Practical_PHP_Programming:Working with temporary files
- 7.7. Practical_PHP_Programming:Seeking through files
- 7.8. Practical_PHP_Programming:Checking whether files exist
- 7.9. Practical_PHP_Programming:Retrieving a file's status
- 7.10. Practical_PHP_Programming:Breaking up a filename into parts
- 7.11. Practical_PHP_Programming:A working example: making a counter
- 7.12. Practical_PHP_Programming:Handling file uploads
- 7.13. Practical_PHP_Programming:Locking files for use
- 7.14. Practical_PHP_Programming:File permissions
- 7.15. Practical_PHP_Programming:Working with directories
- 7.16. Practical_PHP_Programming:Working with remote files
- 7.17. Practical_PHP_Programming:Checksumming your files
- 7.18. Practical_PHP_Programming:Parsing a configuration file
