Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8842533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:58:57+00:00 2026-06-14T10:58:57+00:00

I am trying to handle duplicate filenames. if a user uploads a file name

  • 0

I am trying to handle duplicate filenames. if a user uploads a file name called “file” then another user uploads a file called “file” I want to be able to add a 1 to the new file so it would be now called “1file”. I am able to do this but my issue is when someone uploades “file” for a third time it overrides “1file” I need the 1 to increase. I am using The yii framework

Here is my function in my controller:

function actionIndex(){
  //$dir = Yii::getPathOfAlias('application.images');
  //$dir is a variable that will hold the files uploaded
  $dir = Yii::app()->controller->module->cssFolder;
  //uploaded is a flag to see if we need to display a success/error message
  $uploaded = false;
  $model=new CssUpload();
  if(isset($_POST['CssUpload'])){
      $model->attributes=$_POST['CssUpload'];
      //see if file is correct format and allows us to see and save the file
      $file=CUploadedFile::getInstance($model,'file');
      if($model->validate()){
         $i = 0;
         //trying to add a int to the front/end of a duplicate file
         //able to handle the duplicate one time then gets stuck in an infinete loop
         //(it actually breaks on the first duplictae but it uploads)
        do{
            if(file_exists($dir.'/'.$file)){
                    //increase $i by 1 every time it loops through
                   $i++;
                   //for test purposes only
                   echo $i;
                  //upload the file
                  $uploaded = $file->saveAs($dir.'/'.$i.$file->getName());
                   //stops the infinute loop
                   break;
           }

       }while(file_exists($dir.'/'.$file));
         //upload the original file with no new extensions
         $uploaded = $file->saveAs($dir.'/'.$file->getName());

    }
  }

  $this->render('index', array(
     'model' => $model,
     'uploaded' => $uploaded,
     'dir' => $dir,
  ));

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:58:59+00:00Added an answer on June 14, 2026 at 10:58 am

    Well I think you have two options:

    • append something like microtime() to the end of the filename, that way it’s virtually impossible for you to have the same file name twice.

    • if you need the files to be kept in order and don’t want to use the approach above, then you’d have to store the last inserted number in a database and then retrieve it, increment and store again.

    I use the YII framework as well, but can’t remember of anything specific to Yii that would help you here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to handle a file address within my python program and pass it
I'm trying to handle the problem where a user can try to open, with
Possible Duplicate: reading a specific file from sdcard in android I'm trying to make
I am trying to handle a duplicate key exception while I insert using batchupdate
Possible Duplicate: Parse a JavaScript file through PHP I've been trying to pass a
I'm trying to serialize an object that have a duplicate node name using C#,
I'm trying handle bad json data when parsed through json_decode(). I'm using the following
I'm trying to handle a JSONP Callback, I have the following JavaScript var URL
I am trying to handle a case of a DateTime which might be null
I am trying to handle a contenteditable body in an iframe, in order to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.