View Full Version : Searching for a file?
Y-STU-K
03-20-2005, 11:36 PM
Hi
I'm trying to devise a way of searching for files of a certain type, so that could be jpeg's for example.
Now the user can upload to a directory and store their files in what ever directory structure they wish within their own directory.
What my script needs to do is traverse those directories/files to find and return a list of those files.
Now heres my little issue the user can delete those files at will using an FTP program, as well as using the web site, so from that point of view I can't use a database to store them.
I'm currently trying to work out how to traverse these directories and i've come out with a messy way with lots of loops which only works for 2 levels. I'm sure theres a better way but I can't work it out.
My other idea is to have a txt file which acts as a manifest, and its updated when the system can't find a file, but I'm not loving that idea.
Does anyone have any better solutions for this problem? or better still a more efficient way of my first solution so that I can go down as many dir levels as is needed?
Thanks
Stu :)
chump2877
03-20-2005, 11:40 PM
Let's see the code you've written so far...you might not have to trash it all....
Y-STU-K
03-20-2005, 11:52 PM
Heres what I have so far which is causing me alot of issues, i'm trying to update the array GLOBALS['dir_list'] with the directories inside pos 0 of the array and then use the internal array pointer to increment along the array, but I guess it doesn't do it dynamically :( and my code is infinate looping
session_start();
$GLOBALS['dir_list'][] = 'projects/demo';
$GLOBALS['file_list'];
$GLOBALS['dir_len'] = count($GLOBALS['dir_list']);
get_files();
function get_files()
{
openfolder(current($GLOBALS['dir_list']));
while (current($GLOBALS['dir_list']) != $GLOBALS['dir_len'])
{
openfolder(next($GLOBALS['dir_list']));
}
$num = count($GLOBALS['file_list']);
for ($i=0; $i < $num; $i++)
{
echo $GLOBALS['file_list'][$i];
}
}
function openfolder($filepath)
{
if(is_dir($filepath)) //Check for directory
{
if($dir = opendir($filepath)) //Open directory checking it opens correctly
{
while(($part = readdir($dir)) == true) //Read directory into an array removing . and ..
{
if ($part != '.' && $part != '..')
{
$found = $filepath . '/' . $part;
if(is_dir($found))
{
$GLOBALS['dir_list'][] = $found;
}
else
{
echo $found;
$GLOBALS['file_list'][] = $found;
}
}
}
$GLOBALS['dir_len'] = count($GLOBALS['dir_list']);
}
}
}
NB: I only posted a small bit of it eariler as I was still thinking about how to go about it so sorry for not posting the hole thing
Y-STU-K
03-26-2005, 03:55 PM
*bumped due to update*
Fou-Lu
03-26-2005, 10:13 PM
So let me get this straight, you are simply taking directory A, and searching for any say, .jpg files associated within it, as well as any .jpg files associated within the various sub directories?
Do you require the use of the $GLOBALS directory for other actions within the script? I ask this simply because I see no nessessity to use them.
Also, is this for various file types? Can a user select to search for a .txt file, .php file, .html file, etc?
Y-STU-K
03-26-2005, 10:17 PM
So let me get this straight, you are simply taking directory A, and searching for any say, .jpg files associated within it, as well as any .jpg files associated within the various sub directories?
yes thats correct :)
Do you require the use of the $GLOBALS directory for other actions within the script? I ask this simply because I see no nessessity to use them.
Also, is this for various file types? Can a user select to search for a .txt file, .php file, .html file, etc?
No i don't i put them in for convience while I worked out a better way ;) I decided to try and keep its method as simple as possible. However the script doesn't work anyway it infinate loops.
I'm sure theres a much better solution for doing this, but I still a relatively new programmer and thus I keep every thing as simple as possible, but not always as efficient as possible :(.
Fou-Lu
03-26-2005, 10:27 PM
I'll see what I can do for you, I'm by no means a file manipulation wizard :p
But its the thought that counts ;)
Y-STU-K
03-26-2005, 10:39 PM
I'll see what I can do for you, I'm by no means a file manipulation wizard :p
But its the thought that counts ;)
Thankyou :) i'm not file manipulation wizard either.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.