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
There are several valuable techniques that we learn from this example including:
- 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.
Hi,
I recognize most of my issues are because I was not able to be in class on Cinco de Mayo…a few basic questions:
a. Are we using Zend for this exercise? Can we follow the book example in Chapter 9?
b. I can see the post form with the image I am trying to upload, however the image does not upload. I do get a mail indicating I did send a file. I think it may have to do with the temp folder.
c. I have checked the folders on my servers to make sure they are read/write, they are.
I can’t really get to the meat of the final project without getting the file to upload properly first. I’ll come to class a bit early to see if I can sort out these issues with you or other classmates. Thanks!
@Annette – a) The Cinco de Mayo project does not use ZEND. b) I think this was related to the uploads/uploads issue that we spoke about in class. Let me know if you haven’t been able to get this to work.
In the tshirtprocessor.php
I am not clear what email address (or variable?) should go here?
//create and send an HTML email using MIME type
$to = ‘xxxxxxxxx@gmail.com’;
$subject = ‘PHP File Upload’;
It seems like we want this to go to the email address input by the user but I don’t know how to get that into $to
@Mark – The email address can be retrieved from the processed form like any input element, by referring to the $_POST array using the input element’s name attribute in the associative index ie $_POST['email']
How do I get the size from the $_POST array to display in the email?
What I have done below doesn’t work yet. Must be a way, right?
Thanks
$message = ‘
Thanks for uploading this image
You have ordered a $_POST[size] size shirt
‘ ;
@Mark – For one, you need to pass the name of the input element as a string, such as $_POST['size']. You also need to concatenate the variable name into the text string such as:
$message = “blah blah blah . ” $_POST['size'] . ” blah blah blah”
Yes!
I did wrap the size in single quotes like this ['size'] but until I also wrapped the whole thing
in a another set of single quotes like this, did it finally work for me ‘ .$POST_['size']. ‘
Thanks.
Oh, should we leave all the echo statements on the processor page for you to see if everything works successfully after submitting the form?
Yes!
I did wrap the size in single quotes like this
['size']
but until I also wrapped the whole thing in a another set of single quotes, did it finally work for me like this (double quotes didn’t work)
‘ .$POST_['size']. ‘
Thanks.
Oh, should we leave all the echo statements on the processor page for you to see if everything works successfully after the form is submitted?