charlie
charlie

Reputation: 1384

PHP Search database and directory structure and match together

I have a table with page_name column which lists all my pages like:

directory1/test1.php
directory2/test_page.php
directory3/sub_dir/page5.php

etc.. so it lists the page name and the directory its in

how can i match this list with my directory structure and display all the pages that are not listed in my database table?

so i want to display the files and directories like this:

dir1/file1.php dir1/file2.php dir2/file1.php dir3/file1.phpdir3/file2.php

but only to display if they are not in the database. for example, is dir1/file.php is in the table it will not display on the list

Upvotes: 1

Views: 357

Answers (2)

Shivanshu
Shivanshu

Reputation: 1269

first you may need 'Recursive directory travering', I am posting below the code I found,

$rootpath = '.';
$fileinfos = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootpath)
);
foreach($fileinfos as $pathname => $fileinfo) {
    if (!$fileinfo->isFile()) continue;
    var_dump($pathname);
}

that displays like:

string(11) ".\.htaccess"
string(33) ".\dom\xml-attacks\attacks-xml.php"
string(38) ".\dom\xml-attacks\billion-laughs-2.xml"
string(36) ".\dom\xml-attacks\billion-laughs.xml"

and so on,

now roughly you can use,

$sql='SELECT * FROM your_table WHERE $pathname NOT IN ('your_table')';

just below foreach for your task.

Upvotes: 0

Matthew
Matthew

Reputation: 11270

You can get the filenames of a specific directory using PHP's built-in directory functions: http://www.php.net/manual/en/ref.dir.php

You would, for each file, check if it exists in your database and if not, you print that file.

Upvotes: 1

Related Questions