Jump to content

File upload naming problem

Mast3r Betty

Recommended Posts

Hello, I just got into uploading images onto my website, and ran into a little problem with the file names being unique. How would I make it so that when a file is uploaded, it is renamed to a random number (or something equal)?I'm using this nice code from W3;

<?phpif (($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")&& ($_FILES["file"]["size"] < 20000))  {  if ($_FILES["file"]["error"] > 0)    {    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";    }  else    {    echo "Upload: " . $_FILES["file"]["name"] . "<br />";    echo "Type: " . $_FILES["file"]["type"] . "<br />";    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";    if (file_exists("upload/" . $_FILES["file"]["name"]))      {      echo $_FILES["file"]["name"] . " already exists. ";      }    else      {      move_uploaded_file($_FILES["file"]["tmp_name"],      "upload/" . $_FILES["file"]["name"]);      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];      }    }  }else  {  echo "Invalid file";  }?>

Thank you. :)

Link to comment
Share on other sites

you can specify a new name in the move_uploaded_file functionthe second argument of the move_uploaded_file is the destination folder and the file name, so just add a random number

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" .rand(1.999). $_FILES["file"]["name"]);

just be careful not to add the random numbers in the end of the file name, because a picture lets say somepicture.jpg would be somepicture.jpg3213132 and you will not be able to display that picture in your web page

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...