Upload multiple images at once

Discussion in 'PHP' started by +:::Spider25:::+, Oct 2, 2006.

  1. #1
    Hi Guys,
    I need to upload multiple images at once. What can I do?

    Thanx a lot
     
    +:::Spider25:::+, Oct 2, 2006 IP
  2. TomN

    TomN Peon

    Messages:
    493
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I tested this minimally,, it worked for me so it should work for you.
    it uploads the files prefixed with unix timestamp to avoid overwriting files that already exists:
    set $max = to the number of uploads you want to upload at once.
    remember to change the folder it uploads to and to do anything you need to do with the images before you move them.

    
    <?
    	//TLN
    	//$max is the number of uploads you wanna upload at once.
    	$max = 10;
    	if($max<=0){die("set max to an integer above 0");}
    if(ISSET($_FILES['file1'])){
    	#DO something with uploads......
    	#eg.
    	for($i=1;$i<=$max;$i++){
    	$dir=basename($_FILES['file'.$i]['name']);
    		if(move_uploaded_file($_FILES['file'.$i]['tmp_name'],$_SERVER['DOCUMENT_ROOT'] . "/imgfolder/".mktime(). $dir)){
    			echo "<P>Successfully uploaded file $i...</P>";
    			}else{
    			echo "<P>" . $_FILES['file'.$i]['tmp_name'] . " File $i Failed!</p>";
    		}
    	}
    }else{
    			print("<form enctype=\"multipart/form-data\" name=\"uploadForm\" method=\"post\" action=\"" . $_SERVER['PHP_SELF'] . "\">");
    			for($i=1;$i<=$max;$i++){
    			
    			print("<p>file $i: </p><input type=\"file\" name=\"file$i\"><BR>");
    			}
    			print("<BR><input type=\"submit\" name=\"doaktion\" value=\"Do!\"></form>");
    }
    ?>
    
    Code (markup):
     
    TomN, Oct 2, 2006 IP
  3. vdd

    vdd Peon

    Messages:
    34
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    TomN has good code. But it's more simply to use an Array file:
    <input type=\"file\" name=\"file[$i]\">

    imo
     
    vdd, Oct 3, 2006 IP