This recipe creates a small form on the Bug Tracker management page that is solely responsible for uploading one or more bugs to the database. By editing the content of the importtemplate.csv file and selecting it in the import dialog, users can quickly populate the system by loading data straight to the custom database table that was created by the plugin when it was first installed.
In addition to the file upload field, the form contains the usual hidden nonce and action name fields. It also features an enctype property to allow files to be uploaded.
When the user submits a file to be uploaded, the registered callback function first checks to see whether the user who made the submission has appropriate rights and whether the nonce security fields were present as part of the post data. If both of these conditions are met, the recipe goes on to check whether a file has been correctly uploaded to the web server using the array_key_exists function to search through the standard PHP $_FILES global variable. As you can see, the text that it searches for is the name of the file upload field from the form.
If a file has been uploaded, the fopen function opens it and stores a pointer to it in a local variable. After a quick verification of the pointer's existence, the code moves to a while loop to process each line of the incoming file with the fgetcsv function. This function reads one line of the file at a time, analyzes its content to find all of the comma-separated fields that are present, and stores the resulting data in a numeric array.
The rest of the import function creates an array with the imported data and stores it in the database using the wpdb class' insert method, as we have seen in a previous recipe.