Build Your Own DatabaseDriven Website Using PHP amp;amp; MySQL [Electronic resources]

Kevin Yank

نسخه متنی -صفحه : 190/ 84
نمايش فراداده

Chapter 11: Storing Binary Data in MySQL

Overview

In the previous chapter, we developed a system whereby users could upload image files and have them stored on your Web server for display on your site. In developing this system, we had to tackle several issues:

Ensure that non-conflicting file names are generated.

Synchronize stored files with the database by adding an entry for each file as it is uploaded.

Delete old files when their database entries are deleted.

While I've demonstrated that these constraints can be met, after 10 chapters of pushing the advantages of relational databases, you may have been surprised that I didn't opt for a solution where the files were stored in the database.

In cases where you're dealing with relatively small files, for example, head shots for use in a staff directory, the storage of data in MySQL is, in fact, quite practical. In this chapter, I'll demonstrate how to use PHP to store binary files uploaded over the Web in a MySQL database, and how to retrieve those files for download or display.