Sunday, December 30, 2012

PHP Tutorial (Faq's) part-5

PHP Include

A PHP "Include" is a file that is included into another file. This concept allows you to re-use content throughout your website.
For example, you could have a standard header and footer on every page of your website. Instead of copying/pasting this code on to every page, you could simply place your header code into one include file and your footer code into another include file. Then, on every page, all you need to do is refer to the header and footer include files. You can do this either using the include() function or the require() function.
There's only a small difference between the include function and the require function. More on that below.

The include() Function

To include a file using the include() function, you simply call the function (as you would any other function) and insert the file path as a parameter.

Usage Example

Code

The require() Function

Usage of the require() function is exactly the same as the include() function - simply call the function and pass the path of the include file.
The difference between the include() and require() functions is in the way they handle errors. If the included file can't be located, the include() function will still display the rest of the page (as well as an error). The require() function, on the other hand, will simply display an error - it won't display the rest of the page.

Usage Example

Code

 PHP Upload File

You can use PHP to allow your users to upload a file to the server.
To allow users to upload a file to the server, you first need to provide a form for them to specify which file they want to upload. Once they click the submit button of the form, the action page is called. This is the page that needs to contain the PHP code to process the uploaded file.

The Input Form

Before a user can upload a file, you need to provide them with an interface that allows them to select a file and initiate the upload.
The following code is an example of an input form. There are a couple of important things to note about this code:
  • The action attribute points to a .php file. This is the file that will process the uploaded file.
  • There is an attribute called enctype, and its value is multipart/form-data.
  • One of the input fields has type="file".
Code

The Action Page

Once the user uploads a file, the file is uploaded into a temporary directory on the server. If you don't move the file it will disappear. Therefore, your action page needs to move the file to another location where it can stay as long as you want it to.
Whenever a file is uploaded, you can find out certain information about the file including its name, type, size, as well as the name of the temporary file on the server. These details are made available to you via a PHP array called $_FILES.

Displaying Details of the Uploaded File

This code simply displays the details of the uploaded file. It doesn't move the file to another location - we'll get to that next. For now, you can use this code in conjunction with the above input form to demonstrate what happens when you upload a file to the server.
Notice the PHP $_FILES array which contains info about the file. Note that we also divide the file size by 1024 in order to convert it into kb.
(Ignore any carriage returns in this example - each table row should be on one line).
Code

The above code results in something like this:
Client Filename: Water lilies.jpg
File Type: image/jpeg
File Size: 81.830078125 Kb
Name of Temp File: C:\WINDOWS\TEMP\php48B2.tmp

Moving the Temp File

As mentioned, if we want to keep the file on the server, we need to move it to another location (of our choice). The following code demonstrates how to move the file from the temporary location.
Code

Checking for Errors

The $_FILES array includes an item for any errors that may result from the upload. This contains an error code. If there are no errors, the value is zero ( 0 ).
You check this value within an "If" statement. If the value is greater than zero, you know an error has occurred and you can present a user friendly message to the user. Otherwise you can processing the file.
Code

Restricting File Type/Size

Letting your users upload files to your server can be very risky. If you're not careful, you could get users uploading all sorts of files - perhaps including harmful executables etc. You could also find one day that you've run out of disk space because some users have been uploading enormous files.
You can restrict the file types and file sizes by using an "if" statement. If the file type and size are acceptable, processing can continue, otherwise, display a message to the user.
Important Note: This doesn't prevent the temp file from being created. The file needs uploaded to the server before PHP can find out the file size and type. This simply prevents the file from being moved to your "permanent" location - hence the file should disappear and (hopefully) not become a problem. In any case, I recommend that you install good anti-virus software before allowing users to upload files to your server.
Code

 PHP File

In the previous lesson we learned how to enable our users to upload files to the server. In this lesson, we'll learn how to work with files on the server.

Reading a File

The following code can be used to read a file.
Code

Here's an explanation of what's happening:
  1. Before you read a file you need to open it. We're using PHP's fopen() function and providing it with two parameters: the file name and the mode in which it should be opened.
  2. We then loop through the file using PHP's feof() function. This checks for the end of the file. Our loop specifies that, while the end of file has not been reached, process the code inside the loop.
  3. The code inside the loop uses PHP's fgets() function. This function reads the file line by line. We need to put our own break otherwise each line would end up on the same line.
  4. Finally, we close the file using PHP's fclose() function, passing the opened file as a parameter.

Writing to a File

The following code can be used to write to a file.
Code

Here's an explanation of what's happening:
  1. Again, we use PHP's fopen() function and supply it with two parameters: the file name and the mode in which it should be opened.
  2. We then use PHP's fwrite() function to write to the file. We supply two parameters: the opened file, and the text to go inside the file.
  3. Finally, we close the file using PHP's fclose() function, passing the opened file as a parameter.

Appending Data to a File

The previous code overwrites any content that might have been in the file. If you only want to add to the end of the existing data, you can simply change the mode from "w" (for "write") to "a" (for "append").
Code

Deleting a File

To delete a file, you use PHP's unlink() function and provide it with the name/path of the file to delete.
Code

 

No comments:

Post a Comment