A Better Method to Process $_FILES

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dalesmythe
    New Member
    • Feb 2010
    • 2

    A Better Method to Process $_FILES

    When I first looked at adding the ability to upload files to a PHP form, I was pleased to find that the input type=file was available to do this easily. What was not so easy was deciphering the $_FILES array that contains information about the fields. Using Google to find answers to my questions made me realize that I was not the only one with problems. After finally figuring it out, I thought that there had to be a better implementation, so I wrote a class that provides an easier way to access the information.

    Creating an instance of the Files object accesses the information in the $_FILES super global and stores the information in private arrays. It also creates two properties that can be tested: MaxFileSize is the size of the largest file in the $_FILES array, and TotFileSize is the size of all files in the $_FILES array. If you implement size restrictions, you may be able to use these values to stop further processing without looking at any of the details . Two additional public properties are FileCnt and ErrorCnt. These provide the number of fields that have files or errors.

    There are three main methods (or functions) to access the information on uploaded files. All of them have an optional parameter to restrict the information returned to just fields with certain names. If the value is less than the entire name, it will return any field whose left most character match the value (e.g., a value of “upload” will match “upload0”, “upload1”, etc.). If the names of the input fields used the name[] convention, the occurrence of the name will be added to the name internally, so name[0] becomes name0. Calling these methods will look for the next element that satisfies the requirement. If it finds one, it will set public properties which provide specific information about the file and return true. If no element meets the requirement, it will set the public properties about the file to null and return false.

    The methods are:
    • GetFld($filter) - Looks for information about the next input field (optionally filtered by the passed parameter)
    • GetFileFld($fil ter) - Looks for information about the next input field which specified a file and did not have an error (optionally filtered by the passed parameter)
    • GetErrorFld($fi lter) - Looks for information about the next input field with an error (optionally filtered by the passed parameter)


    After calling one of these methods successfully, the following information is available:
    • FieldName - The name of the input field (if the input field name was in the name[] format, it will have the occurrence number appended to the name).
    • Name - The file name of the uploaded file
    • Size - The size of the uploaded file
    • Type - The type of the uploaded file
    • Tmp_Name - The temporary name and path on the server of the uploaded file
    • Error - The error code of the uploaded file


    The reset($filter) method can be called explicitly to change the information returned by the preceding methods, but it is automatically invoked if the value passed to one of the main methods changes from one call to the next.

    The __get method ensures that the properties available are only those that are defined as public. The __get method is automatically invoked whenever you request the value of a property.

    Here is an example of the code that looks for information from input fields with a name starting with ‘upload’:

    Code:
    include ('class.Files.php');
    
    $files = new Files();
    while($files->GetFileFld(“upload”)) {
    	print " Field: " . $files->FieldName . " Name: " . $files->Name . " Type: " . $files->Type . " Size: " . $files->Size . " Tmp_Name: " . $files->Tmp_Name . " Error: " . $files->Error . "<br>";
    }
    unset($files);
    The class file is attached.
    Last edited by Niheel; Sep 1 '24, 06:20 PM.
Working...