Jump to content

trying to get a list of all files and sub directories on drive using PHP & XAMPP


jasonc310771

Recommended Posts

I have been trying all sorts of code from various sites but stil face different issues each time.

I have now got the full path, file name, file size but can not get the creation and modified date due to errors.

<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); session_start();
define('DB_USER', 'root'); define('DB_PASSWORD', ''); define('DB_HOST', 'localhost'); define('DB_NAME', 'mydupcleaner');
  function db_query($mysqli, $query) { $result = $mysqli->query("$query"); return $result;
  }
$mysqli = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error());
define('DBTableName', 'table1');
$_SESSION['c'] = 1;

$dir = "C:/Users/RH/Desktop/test dir for db work";

function find_all_files($dir)
{
    $root = scandir($dir);
    foreach($root as $value)
    {
        if($value === '.' || $value === '..') {continue;}
        if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;}
        foreach(find_all_files("$dir/$value") as $value)
        {
            $result[]=$value;
        }
    }
    return $result;
} 


$it = find_all_files($dir);

foreach($it as $fullpath) {

	if(!is_dir("$fullpath")) {
		$size = filesize($fullpath);
		if ($size > 0 && $size < 1000000) {
echo("-1- " . $fullpath . " " . $size . " " . "<br>");
			$content = file_get_contents($fullpath);
			$md5 = md5($content);
			$ext = pathinfo($fullpath, PATHINFO_EXTENSION);
echo("-2- " . $fullpath . " " . filectime($fullpath)) . "<br>";
$newfullpath = (array)$fullpath;

$vars = get_object_vars($fullpath);
print_r($vars);

			$cdatetime = DateTime::createFromFormat( 'U', filectime($newfullpath));				// error here
			$mdatetime = "";//DateTime::createFromFormat( 'U', filemtime('"'.print_r($fullpath).'"'));		// error here
			$query = "INSERT INTO `" . DBTableName . "` (`md5`,`fullpath`,`size`,`ext`,`createDate`,`ModDate`) VALUES ('" . $mysqli->real_escape_string($md5) . "','" . $mysqli->real_escape_string($fullpath) . "','" . $mysqli->real_escape_string($size) . "','" . $mysqli->real_escape_string($ext) . "','" . $mysqli->real_escape_string($cdatetime) . "','" . $mysqli->real_escape_string($mdatetime) . "')"; db_query($mysqli, $query);
	
echo("-3- " . $query) . "\r\n";
			}
	}

}
exit;
?>

I just can not get the full path to be used to get the dates, its saying that fullpath is an object so i tried to convert to string, then get error that string is given.  How to I get the fullpath from the object to use as a text string for getting the dates?

Edited by jasonc310771
removed the trailing / from the dir string, but still same errors
Link to comment
Share on other sites

ok thank you.  Well after more changes and more... I now have it working, but... now there is a memory issue....  it stops storing after 200 or so files.

Is there a better way to get all file names listed without having to store so much before it is displayed on screen or stored in database ?

Here is what I have.

<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); session_start();
define('DB_USER', 'root'); define('DB_PASSWORD', ''); define('DB_HOST', 'localhost'); define('DB_NAME', 'mydupcleaner'); define('DBTableName', 'table1');
  function db_query($mysqli, $query) { $result = $mysqli->query("$query"); return $result; }
$mysqli = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error());

$dir = "I:/";	// DO NOT ADD the trailing /

function scanDirectories($dir, $allData, $mysqli) {
    
    $invisibleFileNames = array(".", "..", ".htaccess", ".htpasswd");// set filenames invisible if you want
    $dirContent = scandir($dir);// run through content of root directory
    foreach($dirContent as $key => $content) {
        $path = $dir.'/'.$content;// filter all files not accessible
        if(!in_array($content, $invisibleFileNames)) {
            if(is_file($path) && is_readable($path)) {// if content is file & readable, add to array
                $allData[] = $path;// save file name with path
///////////////////////

					$size = filesize($path);
						$content = file_get_contents($path);			$md5 = md5($content);			$ext = pathinfo($path, PATHINFO_EXTENSION);
						$cdatetime = date_format(DateTime::createFromFormat('U', filectime($path)), 'Y-m-d H:i:s');
						$mdatetime = date_format(DateTime::createFromFormat('U', filemtime($path)), 'Y-m-d H:i:s');
						$query = "INSERT INTO `" . DBTableName . "` (`dateAddedToDB`, `md5`,`fullpath`,`size`,`ext`,`createDate`,`ModDate`) VALUES ('" . $mysqli->real_escape_string(date("Y-m-d H:i:s")) . "','" . $mysqli->real_escape_string($md5) . "','" . $mysqli->real_escape_string($path) . "','" . $mysqli->real_escape_string($size) . "','" . $mysqli->real_escape_string($ext) . "',
						'" . $mysqli->real_escape_string($cdatetime) . "','" . $mysqli->real_escape_string($mdatetime) . "')"; db_query($mysqli, $query);
				
//			echo("-4- " . $query) . "<br><br><br>";

///////////////////////
            }elseif(is_dir($path) && is_readable($path)) {// if content is a directory and readable, add path and name
                $allData = scanDirectories($path, $allData, $mysqli);// recursive callback to open new directory
			}
        }
    }
    return $allData;
}

var_dump(scanDirectories($dir, $allData=array(), $mysqli));
?>

 

Link to comment
Share on other sites

I have tried to get my head around this one, tried the following found on the web...

$flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS;// some flags to filter . and .. and follow symlinks
$iterator = new \RecursiveDirectoryIterator($dir, $flags);// create a simple recursive directory iterator
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);// make it a truly recursive iterator

foreach ($iterator as $path) {// iterate over it

How do I stop the directory names showing in the list?  Only have all files from all directories ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.