Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!Doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Multiple File Uploads</title>
- </head>
- <body>
- <form action="" method="POST" enctype="multipart/form-data">
- <input type="file" name="files[]" multiple />
- <input type="submit" value="Upload File">
- </form>
- </body>
- </html>
- <?
- if(!empty($_FILES['files']['name'][0])){
- $files = $_FILES['files'];
- $uploaded = array();
- $failed = array();
- $allowed = array('jpg', 'png', 'gif', 'jpeg');
- foreach($files['name'] as $position => $filename){
- $file_tmp = $files[tmp_name][$position];
- $file_size = $files['size'][$position];
- $file_error = $files['error'][$position];
- $file_ext = explode('.', $filename);
- $file_ext = strtolower(end($file_ext));
- if(in_array($file_ext, $allowed)){
- if($file_error === 0){
- if($file_size <= 2097152){
- $new_file = md5(uniqid('', true)).'.'.$file_ext;
- $file_destination = "uploads/".$new_file;
- if(move_uploaded_file($file_tmp, $file_destination)){
- $uploaded[$position] = $file_destination;
- }else{
- $failed[$position] = "[{$filename}] failed to upload!";
- }//move uploaded files
- }else{
- $failed[$position] = "[{$filename}] is too large!";
- }//file size
- }else{
- $failed[$position] = "[{$filename}] errored with code {$file_error}!";
- }//file error
- }else{
- $failed[$position] = "[{$filename}] file extension '{$file_ext}' is not allowed!";
- }//in_array
- }// foreach ends here
- if(!empty($uploaded)){
- print_r($uploaded);
- }
- if(!empty($failed)){
- print_r($failed);
- }
- }
- ?>
Add Comment
Please, Sign In to add comment