Fileuploads and Name changes

This is a discussion on "Fileuploads and Name changes" within the PHP Forum section. This forum, and the thread "Fileuploads and Name changes are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Dec 21st, 2005, 23:18
New Member
Join Date: Dec 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Fileuploads and Name changes

I am working on an upload script. It is basically a modified one I found online, and I am wondering how to make it add a number to the back of the file name (such as if file.php is in the folder, it will name another upload of it file1.php and so on) if one under that name already exists instead of it just kicking back. It uses the below code to see if it exists already..

PHP: Select all

if(file_exists($upload_dir.$file_name)){
 echo 
"The file <b>$file_name</b> already exists. <br><a href=\"$_SERVER[PHP_SELF]\">back</a>";
              exit();
           } 
I have a CGI script that does this to possibly aid anyone who helps me.

Code: Select all
foreach(@existing_files){
        if($_ eq $filename){
            $exists = 1;
        }
    }
    
    # if it exists, we need to rename the file being uploaded and then recheck it to 
    # make sure the new name does not exist
    if($exists){
        $newnum++; # increment new number (add 1)

        # get the extension
        @file_type   = split(/\./, $filename); # split the dots and add inbetweens to a list
        # put the first element in the $barename var
        $bareName    = $file_type[0]; 
        # we can assume everything after the last . found is the extension
        $file_type   = $file_type[$#file_type]; 
        # $#file_type is the last element (note the pound or hash is used)
        
        # remove all numbers from the end of the $bareName
        $bareName =~ s/\d+$//ig;
        
        # concatenate a new name using the barename + newnum + extension 
        $filename = $bareName . $newnum . '.' . $file_type;
        
        # reset $exists to 0 because the new file name is now being checked
        $exists = 0;

Last edited by Vineman; Dec 21st, 2005 at 23:23.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jan 3rd, 2006, 17:43
Junior Member
Join Date: Jan 2006
Age: 30
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Fileuploads and Name changes

u would do something like:


PHP: Select all

$newName '';
if(
file_exists($upload_dir.$file_name)){
$newName '1';
           } 
and at some point in your upload script you will have

PHP: Select all

    $_file_ explode("."$file_name);
    
$_ext_ strtolower($_file_[count($_file_)-1]);
$file_path $_file_[0] . $newName $_ext_;
move_uploaded_file($temp_name$file_path); 
something like that
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
fileuploads, name, changes

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 16:31.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42