Forum Moderators: coopster

Message Too Old, No Replies

Renaming Uploaded File!

If it already exists!

         

dreamcatcher

6:23 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys,

How do I go about renaming a file thats being uploaded if it already exists?

if (file_exists($path, $file))

?

If I`m uploading multiple files, I assume I`ll need a loop too?

Any help would be appreciated!

:)

coopster

6:33 pm on Sep 24, 2003 (gmt 0)

dreamcatcher

7:08 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And what if wanted to keep the original file name but add some kind of prefix to the name. ie:

image.gif

to

001_image.gif

Thanks for the help!

jatar_k

7:17 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



who's doing the uploading? Is it users or you?

dreamcatcher

7:20 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The user would be doing the uploading. At the moment I have it so that an error is generated if the file already exists. If two users upload a file with the same file name, I figured it might be useful if it just adds a prefix, rather than displaying the error.

jatar_k

7:21 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I use a form like so where the number of uploads is dynamic

$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>&nbsp;</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.

jatar_k

7:26 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



plus i stripped it out of something else so I may have forgotten some thing(s) ;)

dreamcatcher

7:50 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks jatar_k. I already had the loops and stuff for the upload fields, so I just added the rand stuff and its working great. If the file is already there it adds the number before it. Cool.

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?

dreamcatcher

7:56 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about if I do a timestamp instead?

$randval = time();

$newname = $randval . $_FILES["userfile" . $filecounter]['name'];

?

jatar_k

8:06 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sure, whatever works. I dont use original names for uploads anyway. I use member id's combined with an img id or some such combo. I always limit uploads so it is handled in db usually.

That way the img name ends up beind a combined key and gives me all the data i need about member or img. ;)

coopster

9:06 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Another area that I control in my scripts is I do not allow the user to set the extension (they could upload their own .php script this way), nor do I let them upload a file beginning with "index", nor do I allow any files beginning with a period (.) or other *bad* characters. Just a few other things to keep in mind ;)

dreamcatcher

9:40 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the advice and help guys, and thanks for the code jatar_k, without that I would have been stuck!

:)