In class on Thursday May 5 we started building a PHP form for a simple application that allows a user to upload an image and order a custom Cinco de Mayo tshirt with the image on the front. Using the core PHP libraries, we learned how to upload the file, write a record to a log file, write a record to a MySQL table and send an HTML email with the image included.
All of the code and required files are included here. The scripts/tshirtprocessor.php is documented with comments. The accounts and passwords are deleted and need to be replaced with your own. The example will not work on student.santarosa.edu because the accounts are restricted from uploading files. The dependencies on the server are:
- an uploads/ folder with read/write privileges
- an uploads/files.csv text file (with read/write privileges
- a MySQL account with write privileges
- a MySQL table named files with a filename varchar() column
- sendmail access on the server
- The $_FILES array
- The use of print_r() to display complex variables such as objects and arrays.
- How PHP processes uploads: When a input element of type ‘file’ is posted, it is moved to the server’s /tmp folder, assigned a temporary name, and it’s metadata is posted to the $_FILES array. move_uploaded_file() is the PHP function that allows you to save the file in a specific folder on your site permanently for further reference.
- How to open, write to and close a text file on the server.
- How to format and send HTML email.
- How to connect to a MySQL database and insert a record into a table using the basic PHP libraries.
There are some additional features needed to complete the example and various ways to enhance it with new features. We will be enhancing this contact form in the next few weeks as a final project. At a minimum, we need to:
- Hooking up the additional form elements to the responder, database table, file log & email.
- Further styling of the elements in the form, the response, the email.
- Reports listing the tshirt transactions from the database and the logfile.