copying files from an array using for loop.
Posted: Mon May 25, 2009 4:15 pm
Hey guys im a beginning level php dev.
my task is very simple, I have 20 files being uploaded using a form. ( file1 - file20 )
I want to use a for loop to copy the posted files into my server dir.
I would like to use this array ^
to capture my filenames.
but when I use the expression $file[ $i ] in my for loop. my script crashes. Im running php and apache off of my local machine. Ive tried setting it to another variable, and echoing it. but even when the name outputs correctly ( ie. file1 )
the script still crashes.
it works fine when I use file1, or file6, or whatever the respective file name.
this seems like a syntax problem, but any help would be appreciated. there must be a way to use foor loops with copy().
my task is very simple, I have 20 files being uploaded using a form. ( file1 - file20 )
I want to use a for loop to copy the posted files into my server dir.
Code: Select all
$file = array( "file1", "file2", "file3", "file4", "file5", "file6", "file7", "file8", "file9", "file10", "file11", "file12", "file13", "file14", "file15", "file16", "file17", "file18", "file18", "file19", "file20");
to capture my filenames.
but when I use the expression $file[ $i ] in my for loop. my script crashes. Im running php and apache off of my local machine. Ive tried setting it to another variable, and echoing it. but even when the name outputs correctly ( ie. file1 )
the script still crashes.
it works fine when I use file1, or file6, or whatever the respective file name.
Code: Select all
for ( $i = 1; $i < 20; $i++ )
{
$filename = $file[ $i ];
if ( $_FILES[$filename]['name'] != "" )
{
copy ($_FILES[$filename]['tmp_name'],
"" . $_FILES[$filename]['name'] )
or die ( "Could not copy file" );
}
else{ die( "No file specified" ); }
}