Jump to content

Archived

This topic is now archived and is closed to further replies.

bizerk

Making file a RANDOM NAME on upload.

Recommended Posts

Okay so i have script that if you check a box it overwrites a file withanother file.

here is the code:

[code]f($user_dat['usedspace'] < $dirsize)

{

if(file_exists($user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name']))

{

if($_POST['overwrite_file'] == true)

{

unlink($user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name']);

} else {

$error_occured = true;



echo "The image you attempted to upload already exists!<br />Please select to overwrite exisiting images, or rename the image you're attempting to upload.<br /><br />";

}

}

} else {

$error_occured = true;

echo "You have used up all your directory space.<br />";

}

} else {

$error_occured = true;



echo "Your directory is non-existant. Please contact an adminstrater.<br />";

}

[/code]

-What I want to do is make it so when a user uploads a file with the same name(file_exsists) it will RENAME the $whichfile(file uploaded) to a randomnumber(rand(1,500000). So is this what i should do:

[code]if(file_exists($user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name']))

{

\ rename($user_dat['usrdir'] . "/" . $_FILES[$whichfile]['name'], $user_dat['usrdir'] . "/" . rand(1,5000) . "." . strtolower($split_img[1]));

} else {

$error_occured = true;



echo "The image you attempted to upload already exists!<br />Please select to overwrite exisiting images, or rename the image you're attempting to upload.<br /><br />";

}

[/code]

Share this post


Link to post
Share on other sites
You could use something like this to rename a file:

[code]
function rename_filename($whichfile)
{
$random_number = rand(100000, 999999);
$hour = date ("Hms");//234012
                $info = pathinfo($whichfile);
return "usrfile_" . $random_number . "_" . $hour . '.' . $info['extension'];
}
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.