upload multi files

Discussion in 'PHP' started by roice, Nov 8, 2011.

  1. #1
    Hello,

    I have form with unknown number of file box:

    <input  type="file" value="" name="field[]" />
    <input  type="file" value="" name="field[]" />
    <input  type="file" value="" name="field[]" />
    ...
    ..
    .
    PHP:
    How do I get them in the server side?
    I tried this without success:

    if ($_POST)
    {
           foreach($_POST as $x => $value) {
    		echo $_FILES[$x]['name'];
    		echo $_FILES[$value]['name'];
    		
           }
    }
    PHP:
    I'm asking because I need it to the upload code part:
    if (move_uploaded_file($_FILES['file']['tmp_name'], $files_folder.$_FILES['file']['name']))
    PHP:
    Thanks in advanced,
    Roi
     
    roice, Nov 8, 2011 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    First of all check enctype attribute of your form. Then try
    
    print_r($_FILES);
    
    PHP:
    to see what structure you have to process.
     
    AsHinE, Nov 8, 2011 IP
  3. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you,
    But what do I need to write instead the question mark in order to upload them:
    $_FILES['???']['tmp_name']
    $_FILES['???']['name']

    move_uploaded_file($_FILES['???']['tmp_name'], $folder.$_FILES['???']['name'])
     
    roice, Nov 8, 2011 IP
  4. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #4
    If you print_r($_FILES) you will see what to put instead of question marks.
     
    AsHinE, Nov 8, 2011 IP
  5. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK, I tried it and this is want I got:

    Array ( [field] => Array ( [name] => Array ( [0] => taxi.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => /tmp/phpGHf0LP ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 77910 ) ) )

    of course it mean for specific image...

    So, How should I write the upload code?
     
    Last edited: Nov 9, 2011
    roice, Nov 9, 2011 IP
  6. proactiv3

    proactiv3 Peon

    Messages:
    55
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    0
    #6
    Have you correctly declared the form's encoding type?

    <form action="" method="POST" enctype="multipart/form-data">
    PHP:

    You should get something like this:

    Array
    (
        [files] => Array
            (
                [name] => Array
                    (
                        [0] => vida.js
                        [1] => xml.xml
                    )
    
                [type] => Array
                    (
                        [0] => application/x-javascript
                        [1] => text/xml
                    )
    
                [tmp_name] => Array
                    (
                        [0] => C:\Users\Diogo\AppData\Local\Temp\php5938.tmp
                        [1] => C:\Users\Diogo\AppData\Local\Temp\php5939.tmp
                    )
    
                [error] => Array
                    (
                        [0] => 0
                        [1] => 0
                    )
    
                [size] => Array
                    (
                        [0] => 7180
                        [1] => 4642
                    )
    
            )
    
    )
    PHP:
     
    proactiv3, Nov 9, 2011 IP
  7. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Here is my code
    You can see that I wroete the FORM code right:

    
    
    <html>
    <head>
    <title></title>
    
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
    <script language="javascript">
    
    function addInput() {
    
        var t = $('<input type="file" value="" name="field[]" /><br />');   
        $('#divInputField').append(t);
         
    
    }
    </script>
    </head>
    <body>
    
    <?PHP
    
    
    if ($_POST)
    {
    	print_r($_FILES);
    
    }
    
    
    ?>
    
    <form action="roi.php" method="POST" enctype="multipart/form-data">
    
    	<input type='button' onclick='addInput()' name='add' value='Add input field' />
    	<br />
    	<input  type="file" value="" name="field[]" />
    	<div id="divInputField"></div>
    
    
    	<br />
    	<input type="submit" value="Submit" />
    </form>
    
    <hr />
    
    
    </body>
    </html>
    
    PHP:
     
    roice, Nov 9, 2011 IP
  8. proactiv3

    proactiv3 Peon

    Messages:
    55
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    0
    #8
    <?php
    define('UPLOAD_DIR', 'C:\\Users\\Diogo\\Desktop\\');
    
    if(!empty($_FILES)){
    	$tmpFiles = $_FILES['files']['tmp_name'];
    	$filesName = $_FILES['files']['name'];
    	
    	$errors = Array();
    	$success = Array();
    	
    	for($i=0; $i<count($tmpFiles); $i++){
    		if(!move_uploaded_file($tmpFiles[$i], constant('UPLOAD_DIR') . $filesName[$i])){
    			$errors[] = 'Couldn\'t move the ' . $filesName[$i] . ' file to the location.';
    		} else {
    			$success[] = 'Successfully move the ' . $filesName[$i] . ' to the new location.';
    		}
    	}
    	
    	print_r($errors); print_r($success);
    }
    ?>
    <!doctype html>
    <html lang="en">
    <head>
    	<meta charset="utf-8" />
    </head>
    <body>
    	<form action="" method="POST" enctype="multipart/form-data">
    		<input type="file" name="files[]" /><br />
    		<input type="file" name="files[]" /><br />
    		<input type="submit" name="submit" value="Submeter" />
    	</form>
    </body>
    </html>
    PHP:
    Sorry hand't quite read your last reply on my last post.

    Does this answer your question?
     
    proactiv3, Nov 9, 2011 IP
  9. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Not exactly,
    lets try this - I want to create foreach loop that will print the name of the files using: $_FILES['???']['name']

    this is my form:
        <form action="index.php" method="POST" enctype="multipart/form-data">
            <input type="file" name="files[]" /><br />
            <input type="file" name="files[]" /><br />
              <input type="file" name="files[]" /><br />
          <input type="file" name="files[]" /><br />
    </form>
    PHP:
     
    roice, Nov 9, 2011 IP
  10. proactiv3

    proactiv3 Peon

    Messages:
    55
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    0
    #10
    <?php
    define('UPLOAD_DIR', 'C:\\Users\\Diogo\\Desktop\\');
    
    if(!empty($_FILES)){
    	foreach($_FILES['files']['name'] as $fileName){
    		echo $fileName . '<br />';
    	}
    }
    ?>
    <!doctype html>
    <html lang="en">
    <head>
    	<meta charset="utf-8" />
    </head>
    <body>
    	<form action="" method="POST" enctype="multipart/form-data">
    		<input type="file" name="files[]" /><br />
    		<input type="file" name="files[]" /><br />
    		<input type="file" name="files[]" /><br />
    		<input type="submit" name="submit" value="Submeter" />
    	</form>
    </body>
    </html>
    PHP:
    $_FILES['files']['name'] will be an array on which to each index will correspond one file name.

    This is pretty much the same to what I did above...
     
    proactiv3, Nov 9, 2011 IP
  11. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    OK, This is more similar to want I need.

    how do I add the string "prog" to the file name before I upload it, using the last code you wrote?

    I use to write: $_FILES['some_file']['name'] ='prog'.$_FILES['some_fil']['name'];
    but now I have Array. so how do I change it?

    In the same subject - how do I check the size of the file in FOREACHE loop?
    I mean, I can't write it like this:
    if ($_FILES["file1"]["size"] <= 1000000)
    PHP:
     
    Last edited: Nov 9, 2011
    roice, Nov 9, 2011 IP
  12. proactiv3

    proactiv3 Peon

    Messages:
    55
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    0
    #12
    <?php
    
    if(!empty($_FILES)){
    	$myFiles = array();
    	
    	for($i=0; $i<count($_FILES['files']['name']); $i++){
    		if(!$_FILES['files']['error'][$i] && $_FILES['files']['size'][$i] < 1000000){
    			$myFiles[] = array(
    				'name' => 'prog_' . $_FILES['files']['name'][$i],
    				'tmp_name' => $_FILES['files']['tmp_name'][$i]
    			);
    		}
    	}
    	
    	print_r($myFiles);
    }
    PHP:
    $myFiles will look like this:

    Array
    (
        [0] => Array
            (
                [name] => prog_facebox.js
                [tmp_name] => C:\Users\Diogo\AppData\Local\Temp\php46E0.tmp
            )
    
        [1] => Array
            (
                [name] => prog_jquery.pngfix.js
                [tmp_name] => C:\Users\Diogo\AppData\Local\Temp\php4700.tmp
            )
    
        [2] => Array
            (
                [name] => prog_jquery.wysiwyg.js
                [tmp_name] => C:\Users\Diogo\AppData\Local\Temp\php4711.tmp
            )
    )
    PHP:
     
    proactiv3, Nov 9, 2011 IP
  13. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    OK,
    I found something in your code and modified it in main and succeed

    if(!empty($_FILES)){
        foreach($_FILES['field']['name'] as $x => $fileName){
    		echo $_FILES['field']['name'][$x] . '<br />';
        }
    }
    PHP:
    I just needed to add [$x] to $_FILES['field']['name'][$x]

    THANKS!
     
    roice, Nov 9, 2011 IP