# Rename a file

**URL:** <https://forum.kirupa.com/t/rename-a-file/259550>\
**Category:** Uncategorized\
**Created:** [May 6, 2008, 4:32pm UTC](https://forum.kirupa.com/t/rename-a-file/259550 "2008-05-06T16:32:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonhardwick](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@jasonhardwick](https://forum.kirupa.com/u/jasonhardwick)\
**Post date:** [May 6, 2008, 4:32pm UTC](https://forum.kirupa.com/t/rename-a-file/259550/1 "2008-05-06T16:32:55Z")

</div>

I found this script for renaming files when i upload them how can i change the file name to an auto incremented number and remove the name that the user has given it

IE.  
my 1st photo.jpg = 1002034.jpg

```php
<?php

// Your file name you are uploading 
$file_name = $HTTP_POST_FILES['ufile']['name'];

// random 4 digit to add to our file name 
// some people use date and time in stead of random digit 
$random_digit=rand(0000,9999);

//combine random digit to you file name to create new file name
//use dot (.) to combile these two variables

$new_file_name=$random_digit.$file_name;

//set where you want to store files
//in this example we keep file in folder upload 
//$new_file_name = new upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "file/".$new_file_name;
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";

//$new_file_name = new file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$new_file_name."<BR/>"; 
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>"; 
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>"; 
}
else
{
echo "Error";
}
}
?>

```
