本文整理汇总了PHP中DirectoryIterator::setFilenameFormat方法的典型用法代码示例。如果您正苦于以下问题:PHP DirectoryIterator::setFilenameFormat方法的具体用法?PHP DirectoryIterator::setFilenameFormat怎么用?PHP DirectoryIterator::setFilenameFormat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DirectoryIterator
的用法示例。
在下文中一共展示了DirectoryIterator::setFilenameFormat方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
/**
* Run the file rotation
*
* @return array Array of files which have been rotated
* @throws FilenameFormatException
* @throws RotateException
*/
public function run()
{
if (!$this->hasFilenameFormat()) {
throw new FilenameFormatException('You must set a filename format to match files against');
}
$rotated = [];
$dir = new DirectoryIterator($this->getFilenameFormat()->getPath());
$dir->setFilenameFormat($this->getFilenameFormat());
foreach ($dir as $file) {
if ($file->isFile() && $file->isMatch()) {
// Skip if rotate size specified and initial matched file doesn't exceed this
if ($this->hasSizeToRotate()) {
if ($file->getSize() < $this->getSizeToRotate()) {
continue;
}
}
// Rotate files
for ($x = $this->keepNumber; $x--; $x > 0) {
$fileToRotate = $file->getPath() . '/' . $file->getRotatedFilename($x);
if (!file_exists($fileToRotate)) {
continue;
}
if ($x === $this->keepNumber) {
if (!$this->isDryRun()) {
if (!unlink($fileToRotate)) {
throw new RotateException('Cannot delete file: ' . $file->getRotatedFilename($x));
}
}
$rotated[] = $fileToRotate;
} else {
if (!$this->isDryRun()) {
if (!rename($fileToRotate, $file->getPath() . '/' . $file->getRotatedFilename($x + 1))) {
throw new RotateException('Cannot rotate file: ' . $file->getRotatedFilename($x));
}
}
$rotated[] = $fileToRotate;
}
}
if (!$this->isDryRun()) {
if (!rename($file->getPath() . '/' . $file->getBasename(), $file->getPath() . '/' . $file->getRotatedFilename(1))) {
throw new RotateException('Cannot rotate file: ' . $file->getBasename());
}
}
$rotated[] = $file->getPath() . '/' . $file->getBasename();
}
}
return $rotated;
}