Jump to content

Making file a RANDOM NAME on upload.


bizerk

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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.