Page 1 of 1

Help: displaying fresh results after renaming files.

Posted: Tue Nov 09, 2004 3:40 pm
by Sir-Alucard
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:

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);
	}
?>
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.

Re: Help: displaying fresh results after renaming files.

Posted: Tue Nov 09, 2004 4:25 pm
by timvw
To get started: this script will gives you problems when 2 persons at the same time try to delete something or when someone is deleting, while another is trying to view the pictures

A solution could be: before you delete you put a .lock file in the directory, and when you are ready, you delete the file. Now each function can test if the directory is locked.

Sir-Alucard wrote:

Code: Select all

function renameFiles($folds,$name,$num){
  foreach($files as $n => $fl){
    rename($fl, "./$fo/$name/".$b.($n+1).".jpg");
If i understand well, you rename ALL pics to number+1
You should only do this if $n > $num


If you want to make the script more portable, you might want to use the constant PATH_SEPARATOR instead of /

Code: Select all

<?php
echo DIRECTORY_SEPARATOR;
?>

Posted: Wed Nov 10, 2004 12:44 am
by Sir-Alucard
Thanks about the DIRECTORY_SEPARATOR, I didn't know about it. About the lock to the files it is not necessary for the moment because this is a program designed to be used just by one person at a time, but I will check it out because I should do it that way.

I checked again the program and it is deleting now the correct files, my only problem is when the page reloads the results, it is not showing the fresh ones, just sometimes. I tried using ob_start() but it only worked the first run but then it didn't worked anymore, I needed to open a new window and run it again to make it work again but just the first time again.

If it is not much to ask and if u have time can you talk me more about that "lock file" or maybe tell me where to read about it?
Thank you in advance and that is true also about the $n>$num, and yes, that was what I meant.

(Sorry for my english)

Posted: Wed Nov 10, 2004 3:28 am
by Sir-Alucard
Hi again, I am still having that problem displaying my fresh contents :( , but about the renames and deletions everything is now working very well.

In my rename function I have changed it to:

Code: Select all

function renameFiles($folds,$name,$from){//$from = id# of the first file to be deleted
		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
			
			$n = $from;//if file doesnt exists keep this number to rename next encounter (for multiple deletions)
			//this is my fix to rename from the first feletion to the last file in folder
			for($i=$from;$i<=count($files);$i++){ 

				if(file_exists($files[$i-1])){
					rename($files[$i-1], "./$fo/$name/".$b.$n.".jpg");//rename files w/ new id# and keep them sorted

					$n++;//new id# for the next existent file
			} 
		}
	}
}
Thanks "timvw" for the advice about start renaming them from the first file deletion.