在PHP中剥离文件名

时间:2015-06-22 14:47:32

标签: php rename

我在PHP中构建版本控制系统。我几乎有一个完全工作的系统,我目前正在添加功能。我的代码将文件重命名为" filename_Ver 0.01"如果用户从html选择框中选择次要修订,则递增.01。如果它是主要版本,则文件将重命名为filename_Ver 1,并为每个主要版本递增1。如果我将两者结合起来,让我们说一个主要和次要的修订,我最终得到一个名为" filename_Ver 1.01.ext"

的文件

在PHP表格中显示文件名很容易,但是我想在一个名为" Version"的单独表格字段中显示版本号。我如何从文件名中删除它,以便将其添加到数据库中?我知道我必须在while循环中做一些事情才能得到我想要的东西。 $file = $dr.$fName.'_'.'Ver '.$i.$ext;我希望它显示如下内容:

FileName | Version
test_Ver 0.1 | 0.1
test_Ver 0.2 |  0.2
test_Ver 1   |  1
test_Ver 1.1 |  1.1
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 = 1;

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

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

    $i++;
  }

  return $file;
}
function get_file_name_major_latest($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 = 1;

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

    if(!file_exists($file)){
      $exist = TRUE;
      return $i;
    }

    $i++;
  }

  return 0;
}
function update_file_name_minor($file, $latest_major = 0) 
{
  $pos = strrpos($file,'.');
  $ext = substr($file,$pos);
  $dir = strrpos($file,'/');
  $dr = str_replace(end(explode("/",$file)), '', $file); 

  $arr = explode('/',$file);
  $fName = current(explode(".",end(explode("/",$file))));

  $exist = FALSE;
  $i = (float) $latest_major. '.01';

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

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

    $i += 0.01;
  }

  return $file;
}

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

// variables
define('UPLOAD_DIR', 'repository/'); 
$fileName = $_FILES['file'];
$file_type = $_FILES['file']['type'];
$projectname=$_POST['projectname'];
$comments=$_POST['comments'];

if($_POST['rev_type'] == 'Minor') {
// check for which action should be taken if file already exist
if(file_exists(UPLOAD_DIR . $fileName['name'])) 
{
    $latest_major = 0;
    if(update_file_name_major(UPLOAD_DIR.$fileName['name']) != UPLOAD_DIR.$fileName['name']){
        $latest_major = get_file_name_major_latest(UPLOAD_DIR.$fileName['name']);
        --$latest_major;
    }
  $updatedFileName = update_file_name_minor(UPLOAD_DIR.$fileName['name'], $latest_major);
  move_uploaded_file($fileName['tmp_name'], $updatedFileName);

    include 'db.php';
    $updatedFileName2 = update_file_name_minor($fileName['name'], $latest_major);   
    $add = mysql_query("INSERT INTO `myversions`(filename, filetype, projectname, comments, created) 
    VALUES ('$updatedFileName2', '$file_type','$projectname','$comments', NOW())");

  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') {
// 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);

include 'db.php';
    $updatedFileName2 = update_file_name_major($fileName['name']);
    $add = mysql_query("INSERT INTO `myversions`(filename, filetype, projectname, comments, created) 
    VALUES ('$updatedFileName2', '$file_type','$projectname','$comments', NOW())");

  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

0 个答案:

没有答案