using arrays when uploading a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iwiniquit
Forum Newbie
Posts: 5
Joined: Sat Apr 28, 2007 12:31 pm

using arrays when uploading a file

Post by iwiniquit »

Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i'm trying to upload multiple files using arrays..which works fine..but i'm specifically trying to define a variable based on the number of the array.

here's my code to see what i mean:

Code: Select all

if ($_POST['action'] == 'editmultiple')
        $files = array();
    foreach ($_FILES['photosnews'] as $k => $l) {    
        foreach ($l as $i => $v) {
            if (!array_key_exists($i, $files))
                $files[$i] = array();
            $files[$i][$k] = $v;
              ##### THIS IS WHERE I'M HAVING TROUBLE ####
            if ($_FILES['photosnews'] == 1) {
                $vartest = "this is one";
            } else {
                $vartest = "this is not one";
            }
              #################################
        }
    }
so my logic is that each file has a specific array number starting from 0..when the file has an array number of 1, i want to set a variable to something..and then if it's 2, i want it to set to something different..i'm just not sure what to put in that if statement so that it knows what array number does what. i'm thinking maybe a for statement would be better..but i'm not sure how to get it work with what i have so far.

here's part of the form too if needed:

Code: Select all

<input name="photosnews[]" type="file" class="input" size="10" accept="image/gif, image/jpeg, image/jpg" >
<input type="hidden" name="action" value="editmultiple" />
any help would be greaty appreciated!

-matt


Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you use print_r() or a similar function you will see the structure PHP creates using your field name. It should be straight forward to reach from there.
iwiniquit
Forum Newbie
Posts: 5
Joined: Sat Apr 28, 2007 12:31 pm

Post by iwiniquit »

hey feyd,

that actually helps a lot..but i'm still kinda confused on how i could take that info and make an if statement based on a specific array number.

like if i do this like you said: print_r ($files[1]);

then i get the output of:

Array
(
[name] => DSCN5108.JPG
[type] => image/jpeg
[tmp_name] => /tmp/phpSlvVLX
[error] => 0
[size] => 134564
)

and if i do print_r ($files[$i]);

i'll of course get an output of all of the arrays (total of 3 items, but technically a total of 2 because of the 0)

so what i want to do is make a variable based on a specific number of the array..my logic is something like this:

Code: Select all

if ($files[$i] == 0)
	{
		$filetest = "this is zero";
	}
	if ($files[$i] == 1)
	{
		$filetest = "this is one";
	}
	if ($files[$i] == 2)
	{
		$filetest = "this is two";
	}
but that of course doesn't make sense because $files[$i] just outputs everything..i basically need ito figure out something that will represent the actual number of the array, rather than all of the data itself. and if i do $i == 0 or whatever, it just sets the value to the total of arrays, which is 2 (because of the zero of course). it seems so simple but i'm going insane over it, haha.

here's some more of my code if that helps:

Code: Select all

if ($_POST['action'] == 'editmultiple')
	$filenamxe = $_FILES['photosnews'];
	$files = array();
	
    foreach ($_FILES['photosnews'] as $k => $l) {	
        foreach ($l as $i => $v) {
            if (!array_key_exists($i, $files)) 
            $files[$i] = array();
            $files[$i][$k] = $v;			
        }
    }

    // now we can loop through $files, and feed each element to the class
    foreach ($files as $file) {
	
	if ($files[$i] == 0)
	{
		$filetest = "this is zero";
	}
	if ($files[$i] == 1)
	{
		$filetest = "this is one";
	}
	if ($files[$i] == 2)
	{
		$filetest = "this is two";
	}
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Why not use a for loop?
iwiniquit
Forum Newbie
Posts: 5
Joined: Sat Apr 28, 2007 12:31 pm

Post by iwiniquit »

i'm not sure how to use a for loop without messing up the variables like $k and $i

any ideas?

-matt
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You've reorganized the incoming data such that

Code: Select all

foreach($files as $index => $filedata)
will work nicely.
iwiniquit
Forum Newbie
Posts: 5
Joined: Sat Apr 28, 2007 12:31 pm

Post by iwiniquit »

hey feyd, not really sure what you mean..

what part should i replace that with? and where is $index and $filedata coming from / what do they do?

-matt
iwiniquit
Forum Newbie
Posts: 5
Joined: Sat Apr 28, 2007 12:31 pm

Post by iwiniquit »

nevermind i figured it out..i ended up just adding a value of 0 to like $x before the foreach statement..and then inside the foreach statement i put $x++.

thanks for all your help!

-matt
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

iwiniquit wrote:hey feyd, not really sure what you mean..

what part should i replace that with? and where is $index and $filedata coming from / what do they do?

-matt
the foreach() loop, as feyd has shown, will create an $index variable, which is the index (be it a number or a string) and the value of that index.
nevermind i figured it out..i ended up just adding a value of 0 to like $x before the foreach statement..and then inside the foreach statement i put $x++.
No need, $index, as mentioned, will contain what key is shown.

I suggest you give foreach()a read
Post Reply