Hi, the problem is that I don't want the code to insert the script name in the database. So I added this

basename($path) != basename($file_path)

but don't work and the name of the script is then inserted in the database.

What this script do is read all the file names & directories and send only the file names to a database.

Videos
music.avi
musix.mp3

Documents
myDoc.doc
resume.doc

scriptname.php

So it should be sending:
music.avi
musix.mp3
myDoc.doc
resume.doc

But it is sending:
music.avi
musix.mp3
myDoc.doc
resume.doc
scriptname.php

...
//// get the name of the script, of this way we can't let it out of the table
$file_path = $_SERVER["SCRIPT_NAME"];
echo basename($file_path);

// RecursiveIteratorIterator accepts the following modes:
//     LEAVES_ONLY = 0  (default)
//     SELF_FIRST  = 1
//     CHILD_FIRST = 2
foreach (new RecursiveIteratorIterator($it, 2) as $path) 
{ // foreach began

if ($path != '.' || $path != '..' || basename($path) != basename($file_path)) //insert only values that correspond to the table
{ // if began
echo "PATH: " .basename($path);

echo "<p>\tBASE: " . basename($file_path) . "</p>";

if ( mysql_query(" SELECT * FROM `$it` ") == false ) // if table don't exist, create it 
	{ // if begans
	mysql_query (" CREATE TABLE IF NOT EXISTS `$it`
				(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, file_name VARCHAR(40) NOT NULL, directory VARCHAR (60) NOT NULL,
				FULLTEXT (file_name) ) ");
	} // if end

if ( !is_dir($path) ) // tells whether the given filename is a directory
   { // if began
	$insert = basename($path);
	//echo "<p><strong>The name of the file: $insert</strong></p>";
 	//echo "\n"; // new line
	$path_trimmed = ltrim($path,'.');
	//echo "\n";
	$dir_safe = mysql_real_escape_string($path_trimmed); // allow to enter escape character in a safe way
	$dir_http = str_replace("\\\\", '/', $dir_safe); // replace '\' with '/'
	// echo "\t The directory: $dir_http";
	mysql_query("INSERT INTO `$it` (file_name, directory)
                VALUES ('$insert','$dir_http')");
	} // if end
		
} // if end

} // foreach end

Dani AI

Generated

The behavior was caused by the logical test: because the exclusion checks were ORed, the condition allowed many items through and the script file still got inserted. Credit to for the correct logical fix that stopped the script name from being added — requiring all exclusion conditions to be true is the right intent.

A more robust approach avoids manual dot checks and fragile basename comparisons. Construct the iterators with the SKIP_DOTS flag so . and .. never appear, and iterate files using SplFileInfo methods to tell files from directories. For example:

$rdi = new RecursiveDirectoryIterator($rootPath, FilesystemIterator::SKIP_DOTS);
$rii = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($rii as $file) {
    if ($file->isFile()) { /* handle file */ }
}

See the iterator reference for details: RecursiveDirectoryIterator and the SKIP_DOTS flag in .

Make path comparisons safer by normalizing with realpath() before comparing the script path and the iterated path, so two files with the same basename in different folders won't be confused. Use an early continue for any skip condition to keep the loop clear.

On the database side, move away from the old mysql_* calls to PDO or mysqli with prepared statements, and add a UNIQUE index on the (file_name, directory) pair to prevent duplicate inserts. See the PDO prepared-statement documentation for migration guidance: PDO prepared statements.

These changes make the file-scan routine simpler, more reliable, and more secure for long-term use — exactly what needs if the script will keep running against different directory layouts.

Recommended Answers

All 2 Replies

if ($path != '.' || $path != '..' || basename($path) != basename($file_path))

change to

if ($path != '.' && $path != '..' && basename($path) != basename($file_path))
commented: Thanks, helpful. +2
if ($path != '.' || $path != '..' || basename($path) != basename($file_path))

change to

if ($path != '.' && $path != '..' && basename($path) != basename($file_path))

You did the trick. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.