I don't know how to explain it very well and the files Im using right now are huge so, this is an example of what I am using:
Code: Select all
<?php
function delete($pics){//$pics is an array with multiple files to be deleted
//it has the following format "FolderName.PicNumber";
foreach($pics as $p){
$p = explode(".", $p);//separates the FolderName from PicNumber
$num = end($p);//PicNumber
$name = $p[0];//FolderName
$folds = array("thumbs","pics");//folders to be searched
foreach($folds as $fo){
$b = ($fo=="thumbs") ? "tn_pic" : "pic" ;//change the name if it is a thumbnail
if(count(glob("./$fo/$name/*")) > 1){//if there is a picture yet in the folder
unlink("./$fo/$name/$b".$num.".jpg");//delete the picture
}
}
}
renameFiles($folds,$name,$num);//call my rename function
}
function renameFiles($folds,$name,$num){
foreach($folds as $fo){
$b = ($fo=="thumbs") ? "tn_pic" : "pic" ;//change the name if it is a thumbnail
$files = glob("./$fo/$name/*.jpg");
$files = sortFilesArray($files,$fo,$name,$b);//call my sort function to have them numerically sorted
foreach($files as $n => $fl){
rename($fl, "./$fo/$name/".$b.($n+1).".jpg");
//rename files and keep them sorted
//example: if I delete pic2 and there were 4 pics in the folder
//pic1.jpg is renamed to pic1.jpg
//pic3.jpg is renamed to pic2.jpg
//pic4.jpg is renamed to pic3.jpg
//---------------------------------------------------------------------
}
}
}
function sortFilesArray($files,$fo,$name,$b){
foreach ($files as $fl){
$files_s[] = end(explode("c", basename($fl))); //returns the remainer after pic (i.e. pic1.jpg -> 1.jpg)
}
sort($files_s, SORT_NUMERIC);
$files = ''; //just to recycle var
foreach ($files_s as $fl){
$files[] = "./$fo/$name/".$b.$fl;
}
return($files);
}
?>