Вот хороший способ сделать это
Использовать следующий код:
with temp as (
select count(field1) as summ , field1
from table_name
group by field1 )
select * from temp where summ = (select max(summ) from temp)
Да, просто откройте каталог и создайте цикл для доступа ко всем изображениям и переименуйте их, например:
<?php
if ($handle = opendir('/path/to/files')) {
while (false !== ($fileName = readdir($handle))) {
$newName = str_replace("SKU#","",$fileName);
rename($fileName, $newName);
}
closedir($handle);
}
?>
Ссылки:
http: // php .net / manual / ru / function.rename.php
Вы также можете использовать этот образец:
$directory = 'img';
$gallery = scandir($directory);
$gallery = preg_grep ('/\.jpg$/i', $gallery);
// print_r($gallery);
foreach ($gallery as $k2 => $v2) {
if (exif_imagetype($directory."/".$v2) == IMAGETYPE_JPEG) {
rename($directory.'/'.$v2, $directory.'/'.str_replace("#SKU","",$v2));
}
}
Ну, используя итераторы:
class SKUFilterIterator extends FilterIterator {
public function accept() {
if (!parent::current()->isFile()) return false;
$name = parent::current()->getFilename();
return strpos($name, 'SKU#') !== false;
}
}
$it = new SkuFilterIterator(
new DirectoryIterator('path/to/files')
);
foreach ($it as $file) {
$newName = str_replace('SKU#', '', $file->getPathname());
rename($file->getPathname(), $newName);
}
FilterIterator в основном отфильтровывает все нефайлы и файлы без SKU#
в них. Затем все, что вы делаете, это итерация, объявление нового имени и переименование файла ...
Или в 5.3+ с использованием нового GlobIterator :
$it = new GlobIterator('path/to/files/*SKU#*');
foreach ($it as $file) {
if (!$file->isFile()) continue; //Only rename files
$newName = str_replace('SKU#', '', $file->getPathname());
rename($file->getPathname(), $newName);
}
кусок торта:
foreach (array_filter(glob("$dir/WV1716B*.jpg") ,"is_file") as $f)
rename ($f, str_replace("SKU#", "", $f));
(или $dir/*.jpg
, если число не имеет значения)
Действия по завершению этого довольно просто:
fopen
, readdir
Небольшой пример:
if ($handle = opendir('/path/to/images'))
{
/* Create a new directory for sanity reasons*/
if(is_directory('/path/to/images/backup'))
{
mkdir('/path/to/images/backup');
}
/*Iterate the files*/
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
if(!strstr($file,"#SKU"))
{
continue; //Skip as it does not contain #SKU
}
copy("/path/to/images/" . $file,"/path/to/images/backup/" . $file);
/*Remove the #SKU*/
$newf = str_replace("#SKU","",$file);
/*Rename the old file accordingly*/
rename("/path/to/images/" . $file,"/path/to/images/" . $newf);
}
}
/*Close the handle*/
closedir($handle);
}