| DrDOS |
06-28-2012 04:54 AM |
Multiple uploads with a single selection.
Multiple uploads with a single selection. I modified a single upload script to allow for as many as twenty uploads in one batch, with one selection. Not all browsers support this so have a backup upload. Twenty uploads is the php default and the combined size can't exceed the maximum file size.
PHP Code:
<!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="content-type"> <script type="text/javascript"> //Alerts to errors. window.onerror=function(msg, url, linenumber){var logerror='Error message: ' + msg + '. Url: ' + url + 'Line Number: ' + linenumber;alert(logerror);return false} </script> <body > <?php //include("./colors.php"); if(isset($_POST['MAX_FILE_SIZE'])){ $newdir = $_POST['newdir']; $dir = './Uploads/'; $upload = $dir.$newdir; if (!file_exists($upload)) {mkdir ($upload); chmod($upload, 0777); } $uploaddir = $upload."/"; $isAllowed = array("image/jpeg","image/png","image/gif","image/bmp","application/octet-stream"); for ( $f = 0 ; $f < count($_FILES['userfile']['tmp_name']) ; $f++ ) { $ext = basename($_FILES['userfile']['type'][$f]); if (move_uploaded_file($_FILES['userfile']['tmp_name'][$f], $uploaddir.$_FILES['userfile']['name'][$f])) { if (in_array ( $_FILES['userfile']['type'][$f] , $isAllowed )) echo "File ".$_FILES['userfile']['name'][$f]." is valid, and was successfully uploaded.\n<br>";} if (!in_array ( $_FILES['userfile']['type'][$f] , $isAllowed )){echo "File ".$_FILES['userfile']['name'][$f]." was not an allowed type.\n<br>Allowed types are jpeg, png, gif and bmp\n<br>";} $img[$f] = $uploaddir.$_FILES['userfile']['name'][$f]; print "<img src=\"".$img[$f]."\"><br>"; }} echo '<pre>'; print_r($_FILES); echo "<br>"; print "</pre>"; ?> <!-- The data encoding type, enctype, MUST be specified as below --> <form enctype="multipart/form-data" action="index.php" method="POST"> Make a new folder: <input type="text" name="newdir" /> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="1048576" /> <!-- Name of input element determines name in $_FILES array --> Send this file: <input name='userfile[]' type='file' multiple=20 /> <input type="submit" value="Send File" /> </form> <br> <a href="./download.php">Download</a> <br><br> </body> </html>
// And the download.php used for testing downloads.
<?php $add = time(); header('Content-type: image/jpeg'); header('Content-Disposition: attachment; filename="output-'.$add.'.jpg"'); readfile('./Uploads/Drew/drew von kern new 8.JPG'); ?>
|