1. Home
  2. Computing & Technology
  3. PHP / MySQL

Renaming PHP Uploads

By Angela Bradley, About.com

3 of 4

A Random File Name

//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
$ran = rand () ;

//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = $ran.".";

//This assigns the subdirectory you want to save into... make sure it exists!
$target = "images/";

//This combines the directory, the random file name, and the extension
$target = $target . $ran2.$ext;

This code uses the rand () function to generate a random number as the file name. Another idea is to use the time () function so that each file is named after its timestamp. It then combines this name with the extension from the original file. We also assign the subdirectory... make sure this actually exists!

3 of 4

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Advanced PHP
  5. Assigning a PHP upload a random name

©2008 About.com, a part of The New York Times Company.

All rights reserved.