Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 27, 2023 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/46816e62c07cbd58a8d03439250df541 to your computer and use it in GitHub Desktop.
Save code-boxx/46816e62c07cbd58a8d03439250df541 to your computer and use it in GitHub Desktop.
PHP Upload Multiple Files

PHP UPLOAD MULTIPLE FILES

https://code-boxx.com/upload-multiple-files-php/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Multiple PHP Upload Demo</title>
</head>
<body>
<form action="2-upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="upfile[]" multiple required>
<input type="submit" value="Upload File">
</form>
</body>
</html>
<?php
// (A) SOME FLAGS
$total = isset($_FILES["upfile"]) ? count($_FILES["upfile"]["name"]) : 0 ;
$status = [];
// (B) PROCESS FILE UPLOAD
if ($total>0) { for ($i=0; $i<$total; $i++) {
$source = $_FILES["upfile"]["tmp_name"][$i];
$destination = $_FILES["upfile"]["name"][$i];
if (move_uploaded_file($source, $destination) === false) {
$status[] = "Error uploading to $destination";
}
}} else { $status[] = "No files uploaded!"; }
/* (C) DONE - WHAT'S NEXT?
if (count($status)==0) {
// REDIRECT TO OK PAGE?
header("Location: http://site.com/somewhere/");
// SHOW AN "OK" PAGE?
require "OK.PHP"
}
// (D) HANDLE ERRORS?
else {
// (D1) SHOW ERRORS?
// print_r($status);
// (D2) REDIRECT BACK TO UPLOAD PAGE?
header("Location: http://site.com/1-upload.html/?error=1");
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment