Forum Moderators: coopster

Message Too Old, No Replies

Looping an array

PHP: for() or foreach()

         

Birdman

11:05 pm on Jan 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm stumped on how to loop through an array. I'm uploading multiple files via php and a form like this:

<input name="userfile[]" type="file" />
<input name="userfile[]" type="file" />
<input name="userfile[]" type="file" />

This next snippit is from the single file version I got somewhere:

if (move_uploaded_file($_FILES['userfile1']['tmp_name'], $uploaddir . $_FILES['userfile1']['name'])) {
print "File is valid, and was successfully uploaded. Here's some more debugging info:\n";
print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}

I need to make it into an array instead of if() else(). I cannot figure out the format. Could someone help me out?

Robber

11:17 pm on Jan 28, 2003 (gmt 0)

10+ Year Member



Could you do something like:

$userfiles = $_FILES['userfile'];
foreach ($userfiles as $file){
if (move_uploaded_file($file['userfile1']['tmp_name'],$uploaddir . $file['userfile1']['name'])) {
print "File is valid, and was successfully uploaded. Here's some more debugging info:\n";
print_r($file);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($file);
}

}

Maybe!

But its been a very long day!

crypto

11:13 am on Jan 29, 2003 (gmt 0)

10+ Year Member



Try this

global $userfile,$userfile_name,$userfile_size;

$max_files=x; //specify the no. of files uploaded

$dir="/xyz";
//location of the directory on the server where
//you want to upload the files

//Here is the loop
for ($i=0;$i<$max_files;$i++)
{
if ($userfile_size[$i] > 0)
{
$filename[$i] = basename($userfile_name[$i]);
$target="$dir/".$filename[$i];
if(!@copy($userfile[$i],$target))
die("Error: A problem occured while uploading $filename[$i]. Aborting...");
}
}

Also note that the directory you specify for the upload
should have write permissions on it.

Birdman

12:33 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the help crypto and robber. I'm still having trouble understanding for() and foreach() loops. I'll give it a go.

jatar_k

5:23 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



for($i=0;$i<5;$i++) {
dostuff;
}

a for loop is used to do something a set number of times, or until a set statement becomes true, and is made up of three different parts.

$i=0;

this is evaluated once at the beginning of the loop. In this case it is used to initialize my counter to 0.

$i<5;

This is the control statement of the the loop. The loop wil continue to execute as long as this statement remains true. It is evaluated at the beginning of each iteration of the loop.

$i++

This is evaluated each time the statements inside the loop are completed. In this case it serves to increase the vale of $i by one.

This particular loop will execute "dostuff;" 5 times.
for loop [php.net]

$test = array('one','two','three','four','five');
foreach($test as $value) {
echo $value;
}

a foreach loop will run through an array or list an equal number of times to the number of elements in the array or list.

$test is the array and I have told it to use the var $value to hold them in so inside the loop I use $value to output my array element values.

foreach [php.net]

that help at all?

Birdman

5:40 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>>that help at all?

Actually, that's the best explanation I've seen yet. Thanks for helping me understand it.

Now I just need to figure out how to strip the empty array values, so it won't loop through them. I'm putting five file input fields on the form, but they may only upload 2 files.

Thanks again!

jatar_k

5:46 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$test = array('one','two','three','four','five');
foreach($test as $value) {
if ($value == "") continue;
echo $value;
}

there are two other commands that can be used in loops.

break and continue.

break [php.net] will "break" out of the loop and not execute it anymore.

continue [php.net] will not execute any other code in the loop and return to the beginning of the loop but still does any incrementing of loop vars like $i++.

Birdman

5:57 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Great! Thank you very much. I still find it difficult to navigate(or understand) the manual.