-
-
Save ncou/cb7451c95ac465d86b8a9ada0dc0b0c4 to your computer and use it in GitHub Desktop.
Script upload multiple file dengan PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Multiple File Upload dengan PHP | Jurnalweb.com</title> | |
</head> | |
<body> | |
<form action="" method="post" enctype="multipart/form-data"> | |
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" /> | |
<input type="submit" value="Upload!"> | |
</form> | |
</body> | |
</html> | |
<?php | |
$format_file = array("jpg", "png", "gif", "zip", "bmp"); | |
$max_file_size = 1024*100; //maksimal 100 kb | |
$path = "uploads/"; // Lokasi folder untuk menampung file | |
$count = 0; | |
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ | |
// Loop $_FILES to exeicute all files | |
foreach ($_FILES['files']['name'] as $f => $name) { | |
if ($_FILES['files']['error'][$f] == 4) { | |
continue; // Skip file if any error found | |
} | |
if ($_FILES['files']['error'][$f] == 0) { | |
if ($_FILES['files']['size'][$f] > $max_file_size) { | |
$message[] = "$name is too large!."; | |
continue; // Skip large files | |
} | |
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $format_file) ){ | |
$message[] = "$name is not a valid format"; | |
continue; // Skip invalid file formats | |
} | |
else{ // No error found! Move uploaded files | |
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name)) | |
$count++; // Number of successfully uploaded file | |
} | |
} | |
} | |
echo 'berhasil upload '.$count.' files'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment