In this tutorial, I am going to write a code dealing with uploading the multiple files at once. Although most of my scripts may be a bit complex, I saved the variables that need to be changed into an external file as it makes easier to adopt the code.

We are going to create 3 files: fields.inc.php, upload_form.php, upload.php and an additional folder for our uploaded files.

//fields.inc.php
$fields = array ( "file1" => "File 1: ", "file2" => "File 2: ", "file3" => "File 3: ", "file4" => "File 4: ", "file5" => "File 5: " );

The file fields.inc.php may be further modified by adding additional variables such as max file size or class (for cascading style sheet) and the way array is structured allows such variables to vary for each field. The simplicity of my code is in the way the script may be modified, as all you need in order to increase the number of files allowed to be uploaded, is adding a new field in fields.inc.php file. A new form field is automatically generated - aacoring the the number of fields specified in fields.inc.php

<table>
<form action="upload.php" method="post" enctype="multipart/form-data">
<?php
require_once("fields.inc.php");
foreach($fields as $field => $value) { echo "<tr><th>$value</th> <td><input type='file'name='$field'></td></tr>"; } ?>
<tr><th></th>
<td><input type="submit" value="Upload" name="file_upload" /></td>
</tr></form></table>

Php file containing an upload form, must obviously have a reference to an array, as upload form is dynamically populated with number of upload fields specified in the array. Form is simply generated trough foreach statement.

<?php
require_once("fields.inc.php");
if ( !isset($_POST['file_upload'])) { echo "<h1>Sorry, no file detected</h2>"; exit; }
foreach($fields as $field => $value) {
$currentFile = $_FILES[$field]['name'];
$tmpName = $_FILES[$field]['tmp_name'];
$size = $_FILES[$field]['size'];
$type = $_FILES[$field]['type'];
$uploadDir = 'uploads/';
$filePath = $uploadDir . $currentFile;
if (!empty($currentFile)) {
if (file_exists($filePath)) {
unlink($filePath);}
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "An error occured. Please try again!";
exit;}
if(!get_magic_quotes_gpc()) {
$fileName = addslashes($fileName);
$filePath = addslashes($filePath); }
} } ?>
<h1>Files have been uploaded successfully!</h1>

Now let's look at upload.php file, it firstly detects whether there are any files waiting to be uploaded, if so just like upload_form.php it executes upload script for every file field specified in field.inc.php. If the field is empty is does not do anything and it automatically proceed to the next field.

The beauty of code below is that it is extremely easy to add a database-query inserting the file information into a database. Also, if it's a blogging type of application and users are allowed to keep maximum number of 5 files / images on a server, files may be replaced in both database (file reference) and on a server. You may also enhance my script by specifying max size of a file or rename an actual file according to its position in a form or add a prefix according to Session.auth.username in case it's a blogging type of application. If you want to find out how to do it, email me and I will write a tutorial on it.

Tagged: arrays  development  file upload  php  php script  script  web programming