Invalid operand type?

im trying toi upload each image in FILES[] wi th g

<?php
...
echo '<pre>';print_r($_FILES);echo '</pre>';
...
	if($_SERVER["REQUEST_METHOD"] == "POST") {
			  		  
	  $len = count($_FILES['Flooplans']['name']);

		for($i = 0; $i < $len; $i++) {

			if (isset($_FILES["Flooplans"]) && $_FILES["Flooplans"]["error"] == 0) { 

				$newfilename = "building_".$_POST['building_id']."_floor_".($x + 1).".png";

				move_uploaded_file($_FILES["Floorplan".($x + 1).""]["tmp_name"], "images/" . $newfilename);

			}
		}
			
	header("location: add_building_success.php?id=".$id."&page=".$_POST['page']."");
	}

?>

Am getting

Array
(
    [Floorplans] => Array
        (
            [name] => Array
                (
                    [0] => server_icon.png
                    [1] => port_icon.png
                )

            [full_path] => Array
                (
                    [0] => server_icon.png
                    [1] => port_icon.png
                )

            [type] => Array
                (
                    [0] => image/png
                    [1] => image/png
                )

            [tmp_name] => Array
                (
                    [0] => C:\xampp\tmp\phpCC57.tmp
                    [1] => C:\xampp\tmp\phpCC58.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 11566
                    [1] => 2399
                )

        )

)

Warning: Undefined array key "Flooplans" in C:\xampp\htdocs\AAT_Flow\locations\sites\buildings\add_floors_engine.php on line 26

I dont see the error?

There’s a typo. Flooplans rather than Floorplans. You’re missing the R.

1 Like

How does this relate to the “Invalid Operand Type” in the title?

The OP first asked a different question and then changed it to something else completely :person_shrugging:

1 Like

So… can we get the new error, @lurtnowski ? (and what is $x ? Have you confused your variable names in your loop?)

Yes, im trying with

	if($_SERVER["REQUEST_METHOD"] == "POST") {
			  		  
	  $len = count($_FILES['Floorplans']['name']);

		for($x = 0; $x < $len; $x++) {

			//if (isset($_FILES["Floorplans"][$x]) && $_FILES["Floorplans"][$x]["error"] == 0) { 

				$newfilename = "building_".$_POST['building_id']."_floor_".($x + 1).".png";

				move_uploaded_file($_FILES["Floorplans"][$x]["tmp_name"], "images/" . $newfilename);

				if(!is_uploaded_file("images/" . $newfilename)) { die('Flooplan (Floor '.($x + 1).') Not uploaded.'); }
			//}
		}
			
	header("location: add_building_success.php?id=".$id."&page=".$_POST['page']."");
	}

and am getting
`Array
(
[Floorplans] => Array
(
[name] => Array
(
[0] => server_icon.png
[1] => chassis_icon.png
[2] => kvm_icon.png
)

        [full_path] => Array
            (
                [0] => server_icon.png
                [1] => chassis_icon.png
                [2] => kvm_icon.png
            )

        [type] => Array
            (
                [0] => image/png
                [1] => image/png
                [2] => image/png
            )

        [tmp_name] => Array
            (
                [0] => C:\xampp\tmp\phpAD4B.tmp
                [1] => C:\xampp\tmp\phpAD5B.tmp
                [2] => C:\xampp\tmp\phpAD5C.tmp
            )

        [error] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
            )

        [size] => Array
            (
                [0] => 11566
                [1] => 3403
                [2] => 25981
            )

    )

)

Warning: Undefined array key 0 in C:\xampp\htdocs\AAT_Flow\locations\sites\buildings\add_floors_engine.php on line 34

I printed out the _FILES array also, isnt there an index (0,1,,2)?

look at the structure of $_FILES, then look at how you’re trying to access it. Can you see the problem?

Is $_FILES[“Floorplans”][0][“tmp_name”] a valid accessor?

(Hint: no, its not. why is it not?)

*Is the [0] index only used in numerrical arrays.

Is the array associative?

I should use

$_FILES['Floorplans']['tmp_name'][0]

insttead?

What happened when you tried?

2 Likes

It worked, I didn’t know th e cor rect way to access the array

You’ve been working with arrays for years now. How come you still struggle with them?

1 Like

So here’s what you do. Look at the output you got from your print_r.

Array
(
	[Floorplans] => Array
	(
		[name] => Array
		(
			[0] => server_icon.png
			[1] => chassis_icon.png
			[2] => kvm_icon.png
		)
		
		[full_path] => Array
		(
			[0] => server_icon.png
			[1] => chassis_icon.png
			[2] => kvm_icon.png
		)
		
		[type] => Array
		(
			[0] => image/png
			[1] => image/png
			[2] => image/png
		)
		
		[tmp_name] => Array
		(
			[0] => C:\xampp\tmp\phpAD4B.tmp
			[1] => C:\xampp\tmp\phpAD5B.tmp
			[2] => C:\xampp\tmp\phpAD5C.tmp
		)
		
		[error] => Array
		(
			[0] => 0
			[1] => 0
			[2] => 0
		)
		
		[size] => Array
		(
			[0] => 11566
			[1] => 3403
			[2] => 25981
		)
		
	)
)

Start at the top. Your goal is to walk to the value “C:\xampp\tmp\phpAD4B.tmp”.

Every time you go through a =>, you have to add to your variable path in PHP. So for example:

$j => array (
    ["A"] => array (
         [0] => "C"
         [1] => "D"
    )
   ["B"] => array (
         ["name"] => array (
               [0] => "Mary"
               [1] => "Jane"
         )
   )
)

If I want to walk to “Jane”, I start at $j.
I dont want anything in “A”, so i skip it.
I want to go down “B”… so I walk over that =>, and my path is now $j[“B”].
I want to go into “name”… so I walk over that =>, and my path is now $j[“B”][“name”].
I want to skip over Mary, and go to Jane… so I walk over that =>, and my path is now $j[“B”][“name”][1].
I am at the value I want. So i stop. $j["B"]["name"][1] will return the value “Jane” to me.

2 Likes

As you are requiring $_POST['building_id'] and counting $_FILES['Floorplans']['name'] they should be part of your outside IF condition to avoid errors if those items are missing.

As pointed out $x is not defined and the way you coded $x + 1 may not have the expected results based on the array given. Even if $x= 1; is defined before the for loop , all images having the same name as $x will always be the same, e.g.

building_21_floor_2.png
building_21_floor_2.png
building_21_floor_2.png

Use ($x++) instead so $x is incremented.

Your move_uploaded_file() path has misspellings and the path does not match the array given.

My version…

echo '<pre>';
print_r($_FILES);
echo '</pre>';

if($_SERVER['REQUEST_METHOD'] == "POST" && !empty($_POST['building_id']) && !empty($_FILES['Floorplans']['name'])) {

	$x=1;	  		  
	$len = count($_FILES['Floorplans']['name']);
	for($i = 0; $i < $len; $i++) {
	
		if($_FILES['Floorplans']['error'][$i] == 0) { 
		
			$newfilename = "building_".$_POST['building_id']."_floor_".($x++).".png";
			move_uploaded_file($_FILES['Floorplans']['tmp_name'][$i], "images/" . $newfilename);	
		}
	}
	
	header("location: add_building_success.php?id=".$id."&page=".$_POST['page']."");
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.