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

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.