Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    php multiple file upload using array or for loop

    I have a working example below of a single file input upload via php and now I'd like to upload multiple files either by iterating through each input type="file".

    I've read that the multiple attribute on the input tag is not supported below IE10 so I think the best way would be to have several input type=files in order to loop through them

    any help is appreciated

    Code:
    	<input type="file" name="FilesUpload1" class="filesUpload" />
    	<input type="file" name="FilesUpload2" class="filesUpload" />
    	<input type="file" name="FilesUpload3" class="filesUpload" />
    
    	<?php
    	//Сheck that we have a file
    	if((!empty($_FILES["FilesUpload1"])) && ($_FILES['FilesUpload1']['error'] == 0)) {
    
    		//Check if the file is JPEG image and it's size is less than 350Kb
    		$filename = basename($_FILES['FilesUpload1']['name']);
    		$ext = substr($filename, strrpos($filename, '.') + 1);
    
    		//check file extension
    		if ((($ext == "gif") 
    		|| ($ext == "jpeg")
    		|| ($ext == "jpg")
    		|| ($ext == "png")
    		|| ($ext == "doc")
    		|| ($ext == "docx")
    		|| ($ext == "rtf")
    		|| ($ext == "txt")			
    		|| ($ext == "pdf"))
    
    		//check file mime
    		&& (($_FILES["FilesUpload1"]["type"] == "image/gif")
    		|| ($_FILES["FilesUpload1"]["type"] == "image/jpeg")
    		|| ($_FILES["FilesUpload1"]["type"] == "image/jpg")
    		|| ($_FILES["FilesUpload1"]["type"] == "image/pjpeg")
    		|| ($_FILES["FilesUpload1"]["type"] == "image/x-png")
    		|| ($_FILES["FilesUpload1"]["type"] == "image/png")
    		|| ($_FILES["FilesUpload1"]["type"] == "application/msword")
    		|| ($_FILES["FilesUpload1"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
    		|| ($_FILES["FilesUpload1"]["type"] == "application/rtf")
    		|| ($_FILES["FilesUpload1"]["type"] == "text/plain")
    		|| ($_FILES["FilesUpload1"]["type"] == "application/pdf"))
    
    
    		//check file size is less than 1048576 bytes [1 MB]
    		&& (($_FILES["FilesUpload1"]["size"] < 1048576))) {
    
    			//Determine the path to which we want to save this file
    			$newname = dirname(__FILE__).'/../entries/'.$_POST["CompanyName"].'-'.$filename;	
    
    			//Check if the file with the same name is already exists on the server
    			if (!file_exists($newname)) {
    			
    				//Attempt to move the uploaded file to it's new place
    				if ((move_uploaded_file($_FILES['FilesUpload1']['tmp_name'],$newname))) {
    					echo "It's done! Your file has been saved.";
    
    				} else {
    					echo "Error: A problem occurred during file upload!";
    				}
    
    			} else {
    				echo "Error: File ".$_FILES["FilesUpload1"]["name"]." already exists";
    			}
    
    		} 
    
    		else {
    			echo "Error: Only .gif, .jpeg, .jpg, .png, .doc, .docx, .rtf, .txt, .pdf files under 1 MB are accepted for upload.";
    		}
    
    	} else {
    		echo "Error: No file uploaded";
    	}
    
    	?>

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,030
    Thanks
    15
    Thanked 240 Times in 240 Posts
    PHP Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="content-type">
    <body >
    <?php echo $_SERVER['PHP_SELF'?>
    <?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 Files" />
    </form> 
    <br>
    <a href="./download.php">Download</a>
    <br><br>
    </body>
    </html>
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    New Coder
    Join Date
    Apr 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks for the reply DrDOS. Whilst im quite new to php I'd rather not rewrite my code but instead wrap my code within a for loop.

    could somebody advise as to how I can edit my existing code to cater for looping through multiple file inputs?

    thanks in advance

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,030
    Thanks
    15
    Thanked 240 Times in 240 Posts
    Quote Originally Posted by omar999 View Post
    thanks for the reply DrDOS. Whilst im quite new to php I'd rather not rewrite my code but instead wrap my code within a for loop.

    could somebody advise as to how I can edit my existing code to cater for looping through multiple file inputs?

    thanks in advance
    You just make these changes.
    PHP Code:
    Change thisSend this file: <input name='userfile[]' type='file'  multiple=20 />

    to this: <br />Send this file: <input name='userfile[]' type='file' /><br />

    And 
    duplicate it for as many inputs as you want
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #5
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,030
    Thanks
    15
    Thanked 240 Times in 240 Posts
    You can use a foreach loop which will ignore the different input names.
    PHP Code:
    if(isset($_POST['MAX_FILE_SIZE']) && $_POST['newdir'] != "" ){
    $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");
    foreach(
    $_FILES as $f)
    {
    if(
    $f['error'] == )
    {
        
    $ext =  basename($f['type']);
        if  (
    move_uploaded_file($f['tmp_name'] , $uploaddir.$f['name'])) {
        if  (
    in_array $f['type'] , $isAllowed )) echo "File ".$f['name']." is valid, and was successfully uploaded.\n<br>";}
        if  (!
    in_array $f['type'] , $isAllowed )){echo "File ".$f['name']." was not an allowed type.\n<br>Allowed types are jpeg, png, gif and bmp\n<br>";}
        
    $img $uploaddir.$f['name'];
        print 
    "<img src=\"".$img."\"><br>";
    }}} 
    I think you can modify that to your purposes with a minimum of fuss.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •