Click to See Complete Forum and Search --> : Problems with a loop
wvmlt
01-04-2005, 04:23 PM
I'm trying to use a bulk uploader with my site but the problem I'm having is it only uploads the first image. It tells me all images are uploaded ok, but it actually only uploads the first image in the queue. Here is part of the code.
if (isset($UploadImages)) {
for($i=1; $i<=10; $i++) {
if (!((strpos($_FILES["FILE$i"]['name'], '.gif')>0) || (strpos($_FILES["FILE$i"]['name'], '.jpg')>0))) continue;
$prefix = gensess(15).'-';
if (!move_uploaded_file(
$_FILES["FILE$i"]['tmp_name'], "../tmpimages/$prefix". $_FILES["FILE$i"]['name']))
{
Now if I take out the second expression in the for loop and make it
for($i=1; ; $i++)
it will upload all the images I add to the queue. Is there anything wrong with doing it this way? Would this cause the server to be overloaded by the script continously looping?
Exuro
01-04-2005, 05:28 PM
Why don't you try something like this instead:
$i = 1;
while (isset($_FILES["FILE$i"])) {
// File upload code goes here
$i++;
}
wvmlt
01-04-2005, 08:23 PM
Originally posted by Exuro
Why don't you try something like this instead:
$i = 1;
while (isset($_FILES["FILE$i"])) {
// File upload code goes here
$i++;
}
I tried using this but I'm not sure how to do it. I kept getting an error saying unexpected "}"
Exuro
01-04-2005, 11:00 PM
Originally posted by wvmlt
I kept getting an error saying unexpected "}"
That means you have an unmatched } somewhere in your code. If you want to post the full function I'd be glad to look at it.
wvmlt
01-05-2005, 03:47 AM
Here's what I'm using but it has the second expression blank
<?
if (isset($UploadImages)) {
for($i=1; ; $i++) {
if (!((strpos($_FILES["FILE$i"]['name'], '.gif')>0) || (strpos($_FILES["FILE$i"]['name'], '.jpg')>0))) continue;
$prefix = gensess(15).'-';
if (!move_uploaded_file(
$_FILES["FILE$i"]['tmp_name'], "../tmpimages/$prefix". $_FILES["FILE$i"]['name']))
{
// echo "<br>WARNING! None file has been uploaded! Here is some debugging info:\n";
// print_r($_FILES['Imagenew']['error']); echo '<br><br>';
}
else
{
$Imagename = addslashes($_FILES["FILE$i"]['name']);
$Imagesize = addslashes($_FILES["FILE$i"]['size']);
dbSQL("INSERT INTO `images` (`Creation_Date`, `User`, `Folder`, `Image_Name`, `Image_Temp_Name`, `Image_Size`) VALUES ".
"(".mtime().", '$reguserid', '$FolderID', '$Imagename', '$prefix$Imagename', '$Imagesize')");
?>
<?=$Imagename ?>
(<?=$Imagesize?>
bytes) uploaded successfully... <br>
<?
};
};
};
?>
<tr>
<td>Folder:
<select name="FolderID" id="Upload_form_mod1_FolderView">
<?
$res = dbSQL("SELECT * FROM `imagefolders` WHERE User=$reguserid");
while ($r = mfa($res)) {
?>
<option value="<?=stripslashes($r['ID'])?>"><?=stripslashes($r['Folder_Name'])?></option>
<? } ?>
Exuro
01-05-2005, 05:15 PM
I didn't get the "Unmatched }" error you were talking about, so either you fixed it or the problem lies in another part of your code...
wvmlt
01-05-2005, 07:38 PM
When I tried replacing
for($i=1; $i<=10; $i++) {
with
$i = 1;
while (isset($_FILES["FILE$i"])) {
// File upload code goes here
$i++;
}
is when I got the error. I don't understand where to add
$i = 1;
while (isset($_FILES["FILE$i"])) {
// File upload code goes here
$i++;
}
into my code.
Exuro
01-08-2005, 05:44 PM
Originally posted by wvmlt
for($i=1; ; $i++)
That should throw you into an infinite loop, but it doesn't? I would think that an error would be thrown as soon as it tried referencing an unset index of $_FILE, so why isn't it doing that? Maybe it has something to do with the semi-colons you place after your code blocks:
Originally posted by wvmlt
<?
};
};
};
?>
All of those semi-colons are unnecessary, so if you take them out maybe your code will work.