Rename file after upload based on criteria

94 views Asked by At

If I upload two files with the same name in PHP, the newest one overrides the older version. To prevent this I wrote the following code. However, it seems to timeout whenever I choose Minor from the HTML selectbox. I'm thinking it has something to do with how my variable $i is assigned. Any ideas?

if ( isset( $_POST['addfile'] ) ) {

// variables
define('UPLOAD_DIR', 'repository/'); 
$fileName = $_FILES['fileToUpload'];

if($_POST['rev_type'] == 'Minor') {

    function update_file_name_minor($file) 
    {
      $pos = strrpos($file,'.');
      $ext = substr($file,$pos); 
      $dir = strrpos($file,'/');
      $dr  = substr($file,0,($dir+1)); 

      $arr = explode('/',$file);
      $fName = substr($arr[(count($arr) - 1)], 0, -strlen($ext));

      $exist = FALSE;
      $i = 0.01;

      while(!$exist)
      {
        $file = $dr.$fName.'_'.'Ver '.$i.$ext;

        if(!file_exists($file))
          $exist = TRUE;

        $i + 0.01;
      }

      return $file;
    }

    // check for which action should be taken if file already exist
    if(file_exists(UPLOAD_DIR . $fileName['name'])) 
    {
      $updatedFileName = update_file_name_minor(UPLOAD_DIR.$fileName['name']);
      move_uploaded_file($fileName['tmp_name'], $updatedFileName);

      echo "You have successfully uploaded and renamed the file as a minor revision.";
    }
    else
    {
      move_uploaded_file($fileName['tmp_name'], UPLOAD_DIR.$fileName['name']);

     echo "You have successfully uploaded the file.";
    }

}

elseif($_POST['rev_type'] == 'Major') {

    function update_file_name_major($file) 
    {
      $pos = strrpos($file,'.');
      $ext = substr($file,$pos); 
      $dir = strrpos($file,'/');
      $dr  = substr($file,0,($dir+1)); 

      $arr = explode('/',$file);
      $fName = substr($arr[(count($arr) - 1)], 0, -strlen($ext));

      $exist = FALSE;
      $i = 2;

      while(!$exist)
      {
        $file = $dr.$fName.'_'.'Ver '.$i.$ext;

        if(!file_exists($file))
          $exist = TRUE;

        $i++;
      }

      return $file;
    }

    // check for which action should be taken if file already exist
    if(file_exists(UPLOAD_DIR . $fileName['name'])) 
    {
      $updatedFileName = update_file_name_major(UPLOAD_DIR.$fileName['name']);
      move_uploaded_file($fileName['tmp_name'], $updatedFileName);

      echo "You have successfully uploaded and renamed the file as a major revision.";
    }
    else
    {
      move_uploaded_file($fileName['tmp_name'], UPLOAD_DIR.$fileName['name']);

     echo "You have successfully uploaded the file.";
    }           
  }         
} //main if
2

There are 2 answers

0
hikaru12 On

$i += 0.1 and changed $fName = current(explode(".",end(explode("/",$file)))); to ensure the filename is correctly being set to the file location.

9
Partha On

You can rename file after upload. Like this.

    function random_string($length) {
           $key = '';
           $keys = array_merge(range(0, 9), range('a', 'z'));

        for ($i = 0; $i < $length; $i++) {
             $key .= $keys[array_rand($keys)];
               }

         return $key;
            }

         $filename_tmp=random_string(8);
         $filename=$filename_tmp.".jpg";
  // $filename is autogenerated unique name with 8 Digit.

 // Then change this line:
        move_uploaded_file($_FILES["image"]["tmp_name"],"UPLOAD_DIR/".$filename);