Forum Moderators: coopster
<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?
$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!
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.
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;
$i<5;
$i++
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?
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!
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++.