Moving a the first file in a directory.

Hello,

I have a script which is supposed to move the first file (alphabetically) in a hidden directory to the public directory. The files are all named AB001001.jpg, AB001002.jpg, AB002001.jpg and so on, with the first 2 characters representing the project (for my reference), the next 3 the chapter and the final 3 the issue. Some of the files are .png, but most a .jpg.

The script has no user output - it’s designed to run through a cronjob.

The code successfully moves a file from the holding folder to the public folder, but not the first one (which initially would be AB001001.jpg, but once this is moved, would be …2.jpg and so on). It always seems to move the files in the same order when I test it, but I can’t work out how it’s choosing that order. And besides, it’s not the right order.

The script is below:
[php]<?php
$files = array();
$dir = opendir(‘projhidden’);
while(($file = readdir($dir)) !== false)
{
if($file !== ‘.’ && $file !== ‘…’ && !is_dir($file))
{
$files[] = $file;
break;
}
}
closedir($dir);
sort($files);
for($i=0; $i<count($files); $i++)
for($b=0; $i<count($files); $i++)
$oldPath = “projhidden/” .$files[$i];
$newPath = “projlive/” .$files[$b];
{
rename ($oldPath,
$newPath);
}
?>
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service