Help: displaying fresh results after renaming files.
Posted: Tue Nov 09, 2004 3:40 pm
Hi, I'm trying to make a script that organizes fotos all by itself (create, delete, rename, etc). The problem is after deleting a file, I try to sort(numerical) and rename them but right after doing this I reload the page again but it doesnt display the correct files and by some reason sometimes a file that has been deleted is again created (I think by the rename) but I dont know why if it was not in the folder. Also it doesnt renames them correctly no matter if it has logic order. I think this also has something to do with the caches or something. Maybe im asking to much, but i just want to know how do I make php to wait until the files are deleted and renamed before displaying the results. I have tryied ob_start() but i dont know how to use it very well when multiple files are going to be deleted. I know that ob_start() will retain my output results in the buffer until a flush is called but how do I do that php waits to rename my files until the desired files are deleted correctly and then output the end result?.
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:
I know it will be difficult to understand what i am trying to do but i "hope" that maybe somebody understands it and be able to help me. Thank you in advance.
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);
}
?>