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

Renaming PHP Uploads

By , About.com Guide

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!
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.