Jump to content

Renaming a file


Recommended Posts

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

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

}

}

?>

   

Link to comment
https://forums.phpfreaks.com/topic/104395-renaming-a-file/
Share on other sites

Well there is another way you can do it...

 

You can create a table in your database which records all the information of the users upload thing. E.G. date uploaded, ip of uploader and id (auto incretement) of upload.

 

Okay then what you can do, is whenever someone uploads a file, put upload info into db. And say i was the first person to upload something. Then my ID in the db would be 1.

 

The in your script, whenever somone uplloaded a file, you could get the ID of the last upload and add one to it and make that the new name (& ID) for this file.

 

Its a bit complicated i know but that was honestly the first thing that came into my mind XD

Link to comment
https://forums.phpfreaks.com/topic/104395-renaming-a-file/#findComment-534452
Share on other sites

hmm well i just decided to code it for you :P

run tihs sql query on your database...

CREATE TABLE `uploads` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`uploaderip` VARCHAR( 50 ) NOT NULL ,
`date` DATETIME NOT NULL ,
`name` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM 

 

and then in your upload file...

<?php

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

$getinfo= mysql_query("SELECT * FROM uploads ORDER BY id DESC");
$getinfo_num= mysql_num_rows($getinfo);

if ($getinfo_num == "0"){
$filename= "1";
}else{
$get= mysql_fetch_object($getinfo);
$filenum= $get->id+2; //to be on the safe side
$filename= "$filenum";
}

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

$new_file_name= "$filename.$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/>";
$ip= $_SERVER['REMOTE_ADDR'];
$date= gmdate('Y-m-d h:i:s');
mysql_query("INSERT INTO `ragingmo_phpgamer`.`uploads` (`id` ,`uploaderip` ,`date` ,`name`) VALUES ('' , '$ip', '$date', '$new_file_name')");

//$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";
}
}
?>

 

i think that should be alryt

Link to comment
https://forums.phpfreaks.com/topic/104395-renaming-a-file/#findComment-534550
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.