UPLOADING FILES Understanding the $_FILES (Web site builder) array What confuses

UPLOADING FILES Understanding the $_FILES array What confuses many people is that their file seems to vanish after it has been uploaded. This is because you can t refer to an uploaded file in the $_POST array in the same way as with text input. PHP transmits the details of uploaded files in a separate superglobal array called, not unreasonably, $_FILES. Moreover, files are uploaded to a temporary folder and are deleted unless you explicitly move them to the desired location. Although this sounds like a nuisance, it s done for a very good reason: you can subject the file to security checks before accepting the upload. The best way to understand how the $_FILES array works is to see it in action. If you have installed a local test environment, you can test everything on your computer. It works in exactly the same way as uploading a file to a remote server. Inspecting the $_FILES array 1. Create a new folder called uploads in the phpsolutions site root. Create a new PHP file called upload.php in the uploads folder, and insert the code from the previous section. Alternatively, copy upload01.php from the download files for this chapter, and rename the file upload.php. 2. Insert the following code right after the closing

tag (it s also in upload02.php):

    

This uses the array_key_exists() function that you met in the previous chapter. It checks whether the $_POST array contains upload, the name attribute of the submit button. If it does, you know the form has been submitted, so you can use print_r() to inspect the $_FILES array. The

 tags make the output easier  to read.   3. Save upload.php and load it into a browser. It should look like Figure 6-1.  4. Click the Browse (or Choose File) button, and select a file on your hard disk. Click  Open (or Choose on a Mac) to close the file selection dialog box, and then click  Upload. On Windows, you should see something similar to Figure 6-2 on the next  page. A Mac should display the same information, although the value of tmp_name  will probably be something like /var/tmp/phpAVSylw.  155    
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Leave a Reply