Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-28-2012, 04:54 AM   PM User | #1
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,234
Thanks: 11
Thanked 157 Times in 157 Posts
DrDOS is infamous around these parts
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($upload0777); }
$uploaddir $upload."/";
$isAllowed = array("image/jpeg","image/png","image/gif","image/bmp","application/octet-stream");
for ( 
$f $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');
?>

Last edited by DrDOS; 06-28-2012 at 03:10 PM..
DrDOS is offline   Reply With Quote
Old 06-28-2012, 07:32 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
include("./colors.php");
Where's that?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 06-28-2012, 03:05 PM   PM User | #3
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,234
Thanks: 11
Thanked 157 Times in 157 Posts
DrDOS is infamous around these parts
Quote:
Originally Posted by abduraooft View Post
Where's that?
So omit that line, and any other superfluous code. Like I don't have to modify the code of people who upload other example files! Jeesh! BTW colors.php writes a select box with all the 'named' colors with their # values. I could upload that too if you want it.
DrDOS is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:09 AM.


Advertisement
Log in to turn off these ads.