PHP multiple file upload Script

Upload multiple files

Multiple uploads use array-shaped entries in $_FILES. Validate every item independently; one valid file should not make the other files trusted.

foreach ($_FILES['files']['error'] ?? [] as $i => $error) {
    if ($error !== UPLOAD_ERR_OK) {
        continue;
    }
    $tmp = $_FILES['files']['tmp_name'][$i];
    $size = $_FILES['files']['size'][$i];
    if ($size > 2 * 1024 * 1024 || !is_uploaded_file($tmp)) {
        continue;
    }
    // Validate MIME type and create a server-side filename before moving.
}

The examples below show additional variations and practical file-handling cases.

Adding Photos to a gallery
We will learn how to upload multiple files using a single form. This is required if you are allowing members to upload more than one file and you don't know how many files you allow them to upload.
Security note: Validate each uploaded item independently. Do not trust the browser-provided MIME type or original filename, and do not use copy() for uploaded temporary files.

Processing multiple uploaded files

$allowed = ['image/jpeg' => 'jpg', 'image/png' => 'png'];
$finfo = new finfo(FILEINFO_MIME_TYPE);
$names = $_FILES['file_up']['name'] ?? [];
foreach ($names as $key => $originalName) {
    $error = $_FILES['file_up']['error'][$key] ?? UPLOAD_ERR_NO_FILE;
    $size = $_FILES['file_up']['size'][$key] ?? 0;
    $tmp = $_FILES['file_up']['tmp_name'][$key] ?? '';
    if ($error !== UPLOAD_ERR_OK || $size > 250000) { continue; }
    $mime = $finfo->file($tmp);
    if (!isset($allowed[$mime])) { continue; }
    $name = bin2hex(random_bytes(16)) . '.' . $allowed[$mime];
    move_uploaded_file($tmp, __DIR__ . '/upload/' . $name);
}

Using iframe to upload multiple files

We are submitting the form to a different page to execute the code for uploading files. Without taking the browser to uploadck.php we can execute the uploading code by keeping one iframe. We will hide the border and only display the output displayed by uploadck.php file within the iframe. The hidden iframe ( without border ) src parameter will execute uploadck.php file.

Here is the change in code ( of upload.php ) to display the file uploading form with iframe.
<form action='uploadck.php' method=post target='myiframe' enctype='multipart/form-data' >
<input type=hidden name=todo value='upload'>
<table >
<tr ><td class='data'>Images</td><td>
<input type=file name='file_up[]' multiple>
<input type=submit value='Upload Images'></td></tr>
</table>
</form>

<iframe name='myiframe' src='uploadck.php' style="overflow: hidden; height: 100%; width: 100%; position: absolute;" frameBorder=\"0\"> 
</iframe>
As this script allows more than one file to upload so this script is likely to take more time than normal execution time. So it is better to set a higher value to manage maximum script execution time for this script.

Photo gallery script

By using file upload script we can develop a image gallery script where users can upload multiple photos. You can read more.
Photo gallery script with picture upload using PHP MySQL
Uploading file by using JQuery Ajax
Without refreshing pages we can upload files along with other input data by using multipart/form-data. The details we enter along with the uploaded file name can be stored in a MySQL database table.
Add Record & Images To MySQL table
Thumbnail Image generation from uploaded files
Php file upload ( single file )




Subscribe to our YouTube Channel here



plus2net.com







meera venugopal

17-09-2014

example code please




PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer