如何重命名压缩文件名?
Posted
技术标签:
【中文标题】如何重命名压缩文件名?【英文标题】:How to rename zipped filename? 【发布时间】:2020-12-22 09:09:43 【问题描述】:所以我有代码可以从 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>ѕеремещаем файл ",$file," размер не изменилс¤; было ",$filesizes[$file]," стало, ".$fsize,";";
move_demo($file, $from_dir, $to_dir);
else
//echo "<br>","размер изменилс¤ у файла ", $file;
else
//echo "<br/>bad file:",$file, ", size = ", $fsize;
$i++;
closedir($demos_dir);
function move_demo($filename, $from_dir, $to_dir)
//echo $filename,"from ",$from_dir," 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);
?>
【问题讨论】:
@KIKOSoftware 感谢您的理解。编辑了第一篇文章和代码。 【参考方案1】:看看rename()。
这是一个 PoC:
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);
【讨论】:
@loydb 感谢您的回答。我收到错误:Invalid argument in /root/arch.php on line 110
。第 110 行如下所示:$moved += (int)rename($src, "$dst$new_filename");
@Erikas 你能转储rename()
之前的变量吗?即var_dump($src, $dst, $filename, $new_filename);
.以上是关于如何重命名压缩文件名?的主要内容,如果未能解决你的问题,请参考以下文章