如何重命名压缩文件名?

问题描述

所以我有代码可以从 1 个目录移动到另一个目录文件并压缩该文件

我需要的: 将压缩文件重命名为第二个“-”符号。

示例:我得到了压缩文件名“SOMETEXT-de_dust2-20123323.dem.zip”。我只需要该文件名是“SOMETEXT.dem.zip” 所以只需删除所有文本直到第二个 -"-"

有什么建议吗? 感谢您帮助我理解代码 :)

我的代码

<?PHP
//error_reporting(E_ALL);
//set_time_limit(0);
$path = "MIX1/cstrike";
$path2 = "/var/www/html/public/";
$to_dirs = array('/demos/');
$from_dirs = array('/demos/');

$filesizes = array();
//первый проход запоминаем размеры
foreach($from_dirs as $from_dir)
    {
    $demos_dir = opendir($path.$from_dir);
    while (false!==($file=readdir($demos_dir)))
        {
        if ($file!='.'&&$file!='..'&&strpos($file,'.dem')!==false)
            {
            $fsize=filesize($path.$from_dir.$file);
            if ($fsize<50000000)
                {
                $filesizes[$file]=$fsize;
                }
            else{
    //          echo "<br/>bad file:",$file,",size = ",$fsize;
                }
            }
        }
    closedir($demos_dir);
    }
//echo date("h:i:s");
sleep(3);
clearstatcache ();
//второй проход пермещаем 
$i=0;
foreach($from_dirs as $from_dir)
    {
    $to_dir=$from_dirs[$i];
    $demos_dir = opendir($path.$from_dir);
    while (false!==($file=readdir($demos_dir)))
        {
        if ($file!='.'&&$file!='..'&&strpos($file,'.dem')!==false)
            {
            $fsize=0;
            $fsize=filesize($path.$from_dir.$file);
            if ($fsize<50000000)
                {
                if ($fsize==$filesizes[$file])
                    {
                    //echo "<br>ѕеремещаем файл "," размер не изменилс¤; было ",$filesizes[$file]," стало,".$fsize,";";
                    move_demo($file,$from_dir,$to_dir);
                    }
                else
                    {
                    //echo "<br>","размер изменилс¤ у файла ",$file;
                    }
                }
            else
                {
                //echo "<br/>bad file:",$fsize;
                }
            }
        }
    $i++;
    closedir($demos_dir);
    }

function move_demo($filename,$to_dir)
{
//echo $filename,"from "," to ",$to_dir,"<br>";
global $path,$path2;
if (file_exists($path2.$to_dir.$filename.".zip"))
    unlink($path2.$to_dir.$filename.".zip");
echo "$path$from_dir$filename\n";
echo "$path2$to_dir$filename\n\n";
$data = file_get_contents($path.$from_dir.$filename);
$gzdata = gzencode($data,9);
unset($data);
$fp = fopen($path2.$to_dir.$filename.".zip","xb+");
//$fp = fopen($path.$to_dir.$filename.".zip")
fwrite($fp,$gzdata);
unset($gzdata);
fclose($fp);
unlink($path.$from_dir.$filename);
}
?>

解决方法

看看rename()

这是一个概念验证:

function move_files($src,$dst)
{
    $dh = opendir($src);
    if (!$dh) {
        return false;
    }

    // Combine the letters before the first dash/hyphen (-),// and the letters after (and including) the first dot/period (.)
    // after the first dash/hyphen (-).
    $regex = '/^(.+?)-(?:.+?)(\..+?\.zip)$/';
    $moved = 0;
    $total = 0;

    while (($filename = readdir($dh)) !== false) {
        if (filetype("{$src}{$filename}") !== 'file') {
            continue;
        }

        if (!preg_match($regex,$filename)) {
            continue;
        }

        $total++;
        $new_filename = preg_replace($regex,"$1$2",$filename);
        $moved += (int)rename($src,"{$dst}{$new_filename}");
    }

    closedir($dh);

    return [$moved,$total];
}

$srcDir = '/src/';
$dstDir = '/dst/';
$res = move_files($src,$dst);

if (!$res) {
    // Error
}

list($moved,$total) = $res;
var_dump($moved,$total);