Hi,
I'm having a script that includes all the files in a directory. The files are calles file1, file2, file3 and so on. So the include script is just a loop starting at 1 and goes on untill theres no files left. But that's not the problem.
The problem is that i'm making an online admin section where I should be able to delete a file. One of the file1, file2, file3 and so on. But if I delete on of this then the include script want loop thru the all. Let's say I delete file2. Then the include script will loop only once and out put file1. But in the directory there are 15 files. Now called file1, file3, file4 and so on because the file file2 is deleted and the include script stops when a file doesn't exist.
I nees to make something that will loop thru and rename alle the files to file1, file2, file3, file4 and so on. So that the include script will work.
One solution i've though of is to set the include script to count have many files there are in the directory and the loop that many times, but i haven't got that to work either.
Could someone please help me??
Loop Thru and Rename Files
Moderator: General Moderators
My algorithm (in pseudocode) might look like this:
If you're sure that you're only going to be deleting one file at a time, this is a nice simple algorithm. If not, you'll have to get fancier.
Code: Select all
$file = 'file1';
while(is_file($file)) {
increment file number;
}
while(is_file(increment file number)) {
$temp = decrement file number;
rename($temp);
}- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
llimllib's code is an algorithm which uses pseudocode which means it isn't a working example but you should be able to make a start using it.
For functions that can be used when working with files and folders:
http://www.php.net/manual/en/ref.filesystem.php
http://www.php.net/manual/en/ref.dir.php
Mac
For functions that can be used when working with files and folders:
http://www.php.net/manual/en/ref.filesystem.php
http://www.php.net/manual/en/ref.dir.php
Mac
where i say 'increment file number', I left that undefined because it sounded like (a) you had figured out how to do it already and (b) that's something I figured you'd want exact control of. In the example system you mentioned, it means turning 'file1' into 'file2', while 'decrement file number' means turning file2 into file1. Another reason I didn't include it is that it probably makes sense to turn these into functions, that way you can tweak it easily.
ok, so I can use the readdir() function and make it loop thru every file. And then include the file to the browser? I've never used but readdir() to include the files into the browser before, just to print the name og the file. The anoying thing about readdir() is that you get
.
..
on the top. Don't know if they get there when I'm including the files in the folder, but I'll try that now. Thanx for the tip
.
Heres the include the files code:
<?php
$x = "1";
while ( $x )
{
$includefile = "produkter/produkt$x".".php";
if( file_exists("$includefile") )
{
echo("<br>");
include("$includefile");
echo("<hr noshade>");
}
else
{
break;
}
$x++;
}
?>
.
..
on the top. Don't know if they get there when I'm including the files in the folder, but I'll try that now. Thanx for the tip
Heres the include the files code:
<?php
$x = "1";
while ( $x )
{
$includefile = "produkter/produkt$x".".php";
if( file_exists("$includefile") )
{
echo("<br>");
include("$includefile");
echo("<hr noshade>");
}
else
{
break;
}
$x++;
}
?>