Jump to content


Photo

Making file a RANDOM NAME on upload.


  • Please log in to reply
1 reply to this topic

#1 bizerk

bizerk
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 07 October 2006 - 05:36 PM

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

here is the 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 />";

		}


-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:

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 />";

					}



#2 yonta

yonta
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 07 October 2006 - 05:54 PM

You could use something like this to rename a file:

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

do it, do it right, do it right now




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users