Current location: Hot Scripts Forums » Programming Languages » PHP » Renaming Uploaded Files


Renaming Uploaded Files

Reply
  #1 (permalink)  
Old 07-26-09, 11:24 PM
..Chris ..Chris is offline
Newbie Coder
 
Join Date: Jul 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Arrow Renaming Uploaded Files

Well currently I have a script that uploads files and displays the URL. That's about all I really want. Only one thing I don't know how to do though. When theres a URL with the same name, it simply over writes the file.

Something that could stop that from happening is if I rename the uploaded files to a random 8-10 number/letter combonation. For example, if someone uploaded a file named my_photo.png, it should be renamed to 8a1f2e55se.png.

Any help is appreciated.
Reply With Quote
  #2 (permalink)  
Old 07-27-09, 12:43 AM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
The easiest way I have found to solve this problem is to upload the file with the original name and then append a 4 digit random number to the end of the file name.

so

picture.jpg

becomes

picture5489.jpg

This makes it extremely unlikely that files will overwrite because not only must the file name be the same, but the 4 digit number would have to be the same, so someone would have to upload picture.jpg 9999 before a overwrite is guaranteed.
Reply With Quote
  #3 (permalink)  
Old 07-27-09, 01:01 AM
DaiLaughing DaiLaughing is offline
Newbie Coder
 
Join Date: Jul 2009
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
You should already be using the FILES to get the old name for the file:

PHP Code:

$oldname=$_FILES['file01']['name']; 

so just add a number to that as ruteckycs said. You might need to explode() the old name (be careful to allow for multiple dots in the name). Then add the number and implode() it back together.
Reply With Quote
  #4 (permalink)  
Old 07-27-09, 04:02 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

$filename  $_FILES['file']['name'];

$extension end(expode('.'$filename));
$chars 'abcdefghijklmnopqrstuvwxyz012346789';

do
{
    
$filename sprintf('%s.%s'substr(010str_shuffle($chars)), $extension);
}
while (
is_file("path/to/files/{$filename}")); 
... untested, but might work.

Last edited by Nico; 07-27-09 at 05:31 AM.
Reply With Quote
  #5 (permalink)  
Old 07-28-09, 02:10 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Rather than a random number, I'd use the time.

(Sample code - in use)

PHP Code:

        $sImage='tmp/'.time();

        
shell_exec('convert -comment "Generated by http://wirehopper.com/design/banner.php" '
                
.$sList.' +append -unsharp 0x.2 '
                
.' +flatten -quality 100 '
                
.$sImage.'.png'); 
Reply With Quote
Reply

Bookmarks

Tags
file, rename, script, upload, url


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
renaming files raymyster Visual Basic 1 12-24-07 03:17 AM
Viewing uploaded files john27 PHP 2 04-24-07 07:28 AM
Check uploaded files size zoliky PHP 5 09-27-06 01:52 PM
Showing Uploaded Files, Help! Should be fairly easy for you! Scientist Script Requests 3 02-04-06 02:17 PM
Upload Script Problem!!! seanknighton Perl 0 03-21-04 09:54 PM


All times are GMT -5. The time now is 07:55 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.