Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2003
    Posts
    6

    Talking Unanswered: Embedding a 'For' Statement in a File Upload

    I am trying to embed a FOR statement into a PHP file upload script that already uses a FOR statement to handle multiple file uploads. I have written the statement below:
    Code:
    for ($counter=1; $counter<=3; $counter++){
    		print "Number: ".($counter*1)."<br>\n";
    And added it to this Upload Script:
    Code:
    $file_dir=$directory;
    
    
    	foreach($_FILES as $file_name => $file_array){
    		
    		for ($counter=1; $counter<=3; $counter++){
    		print "Number: ".($counter*1)."<br>\n";
    		}
    		print "path: ".$file_array['tmp_name']."<br>\n";
    		print "name: ".$file_array['name']."<br>\n";
    		print "type: ".$file_array['type']."<br>\n";
    		print "size: ".$file_array['size']."<br>\n";
    		
    		$result2=mysql_query("INSERT INTO tblimages (item_id, image_name) VALUES('$insert_id', '".$file_array['name']."')")or die("Insert Error: ".mysql_error());
    		
    		if (is_uploaded_file($file_array['tmp_name'])) {
    			move_uploaded_file($file_array['tmp_name'],
    				"$file_dir/$file_array[name]") or die ("Couldn't Copy");
    			print "File was moved!<br><br>";
    			
    			}
    		}
    Which produces the following output for each file:

    Number: 1
    Number: 2
    Number: 3
    path: /var/tmp/phpC19iLS
    name: basket01sm.jpg
    type: image/jpeg
    size: 1436
    File was moved!

    Number: 1
    Number: 2
    Number: 3
    path: /var/tmp/phpvDe0vs
    name: basket02sm.jpg
    type: image/jpeg
    size: 1435
    File was moved!

    Number: 1
    Number: 2
    Number: 3
    path: /var/tmp/phpKtBhPm
    name: basket03sm.jpg
    type: image/jpeg
    size: 1539
    File was moved!
    Record added

    I would like to adjust the For statment so it each file gets a unique number assigned like the following:

    Number: 1
    path: /var/tmp/phpC19iLS
    name: basket01sm.jpg
    type: image/jpeg
    size: 1436
    File was moved!

    Number: 2
    path: /var/tmp/phpvDe0vs
    name: basket02sm.jpg
    type: image/jpeg
    size: 1435
    File was moved!

    Number: 3
    path: /var/tmp/phpKtBhPm
    name: basket03sm.jpg
    type: image/jpeg
    size: 1539
    File was moved!
    Record added

    Any advice greatly appreciated!. Thanks in advance.
    Zanshin

  2. #2
    Join Date
    Jul 2003
    Location
    Ohio/Chicago
    Posts
    75
    I think you're confused on the for each loop. It will go though each file and then do everything inside it for each file in the loop. So for the first file, it does your for loop which prints out 1,2,3. For what you're doing that doesn't make any sense. Declare a variable before the loop starts and initialize it to 1. Then inside the for each loop iterator++ and if you need to check to make sure it doesn't go higher than 3. Hope this makes sense, reply if you need more help.

  3. #3
    Join Date
    Jul 2003
    Posts
    6

    sorry for the silly question:)

    Thanks for answering my question! I got it now....

    regards
    zanshin

    Originally posted by unatratnag
    I think you're confused on the for each loop. It will go though each file and then do everything inside it for each file in the loop. So for the first file, it does your for loop which prints out 1,2,3. For what you're doing that doesn't make any sense. Declare a variable before the loop starts and initialize it to 1. Then inside the for each loop iterator++ and if you need to check to make sure it doesn't go higher than 3. Hope this makes sense, reply if you need more help.

Posting Permissions

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