Forum Moderators: coopster
$max_files // set in the config, max a user is allowed
$num_files // present number of files they have uploaded
this will give them a form with as many upload spots as they have remaining
<?
if ($num_files < $max_files) {
$filesremain = $max_files - $num_files;
?>
<p><form name="filesupform" method="post" action="files-up.php" enctype="multipart/form-data">
<table border="0" cellpadding="2" cellspacing="2">
<?
$upcount = 1;
while ($upcount <= $filesremain) {
echo "<tr><td colspan=\"2\"><b>Image",$upcount,"</b></td></tr>";
echo "<tr><td valign=\"top\">Description ",$upcount,":</td><td><input name=\"description",$upcount,"\" size=\"50\" maxlength=\"80\"></td></tr>";
echo "<tr><td valign=\"top\">File ",$upcount,":</td><td><input type=\"file\" name=\"userfile",$upcount,"\"><br> </td></tr>";
$upcount++;
}
?>
<tr>
<td colspan="2"><input type="button" value="Upload Selected Images"></td>
</tr>
<tr>
</tr>
</table>
</form>
<?
print "</td></tr></table>";
} else {
echo "<P><B>Maximum number of images already added</b>";
}
?>
I don't allow users to name files so much, I usually rename them on upload and give them an ID, then I never worry about duplicates. It's all managed by the script.
but
<?
$filecounter = 1;
while ($filecounter <= $max_files) { // again, $max_files would be in the config
if ($_FILES["userfile" . $filecounter]['name'] == "") {
$filecounter++;
continue;
}
$path = "/where/to/put_it/";
$checkfile = $path . $_FILES["userfile" . $filecounter]['name'];
if (file_exists($checkfile)) {
$randval = rand (1, 10);
$newname = $randval . $_FILES["userfile" . $filecounter]['name'];
} else {
$newname = $_FILES["userfile" . $filecounter]['name'];
}
if (!move_uploaded_file($_FILES["userfile" . $filecounter]['tmp_name'], $path . $newname)) {
echo "<strong>",$_FILES['userfile']['name'],"</strong> did not upload!";
die;
}
}
?>
see if that works, I didn't test it so it may just blow up ;) It has some extra tests in there so you may need to remove them.
Now supposing its added the number 7 before say, print.gif, so we now have 7print.gif, whats stopping that file being over written if another file called print.gif is uploaded and it generates the number 7 again?