bizerk Posted October 7, 2006 Share Posted October 7, 2006 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] Quote Link to comment Share on other sites More sharing options...
yonta Posted October 7, 2006 Share Posted October 7, 2006 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.