Advanced PHP
The Date Function
The Date function allows one to time-stamp a form so one can tell when form entries are made or to simply to show the date on a website.
The Include Function
The include function is simple in it's use but very useful for making simple, universal changes or simply making large groups of code seem simpler.
The Fopen Function
Open File
The "fopen" function lets the PHP open another file to be read and output onscreen using other functions.
Using the function itself is simple, I simply declare a variable and set the value as the "fopen" function with it's parameters set to my file's name and the read/write setting. The read/write setting tells the code what way to treat the file, in this case I use "r" because I only want the code to read the file. |
End of File Check
The end of file check is what is says it is, It checks the file to see if the PHP has reached the end. Using it is simple: one enters the function "feof" an declares the file's variable in parentheses. This is most often used with an if else statement to create a verbose output on whether a file has been opened correctly.
|
Read File Line by Line
For the code to actually read the file that has been opened one must declare one of the functions built to do so, simply telling the code to echo the file's variable will show the resource id of the file.
One of the commands used to read files is "fgets," this function reads a single line of the code and makes it available for use by an "echo" command. Using this function is the same as using the other file commands, declare the function, "fgets," and declare the files variable in parentheses. The code sample I've placed to the right of this paragraph shows the function being used with a while loop that will read the entire file, line by line and then close the file. |
Creating a File Uploader
Creating the form
The form that can upload files is not much different from any other form. As with other forms that use PHP, its action should be set to the PHP file that will manage it, and its method should be set to either "get" or "post," in this case I use "post" as I don't want to show the file's information in the URL. In this particular form I've used a enctype as well to help with security as this does have the server temporarily download the files that are uploaded. After the form parameters one simply needs to create a file input and a submit button.
|
Creating Upload Restrictions
Creating upload restrictions is simply an extended if else statement that has multiple conditions. The file type statements have "| |" next to them to say "or" as in the file could be a "gif" or a "jpeg" or a "png." The filetype listed as "pjpeg" is simply a "jpeg" listed so internet explorer could interpret it. Finally the "& &" statements say "and" as in the files must have both conditions instead of just one or the other.
|
Error Handling
Die Function
The "die" function simply ends all code, preventing it from being run. This is used when the code that is being run could create a large number of errors and cause server problems if not run correctly. Due to the fact that the function stops even the HTML in the code from being run it should be used sparingly to prevent site errors and keep your site from looking bad.
|
Creating a Custom Error Handler
Creating a Custom error handler is necessary as the standard error handler is hard to interpret by experienced users and is hard to verbalize. Creating a custom error handler can make a long explanation of the error into a short uttering of the error number. Creating the handler is simply a special function that is put into another function named "set_error_handler." Using this function tells the code to run the function that was put in the "set_error_handler."
|