core-bundle/src/Resources/contao/library/Contao/Database/Updater.php
public function scanUploadFolder($strPath=null, $pid=null)
	{
		if ($strPath === null)
		{
			$strPath = Config::get('uploadPath');
		}

		$arrMeta = array();
		$arrMapper = array();
		$arrFolders = array();
		$arrFiles = array();
		$rootDir = System::getContainer()->getParameter('kernel.project_dir');
		$arrScan = scan($rootDir . '/' . $strPath);

		foreach ($arrScan as $strFile)
		{
			if (strncmp($strFile, '.', 1) === 0)
			{
				continue;
			}

			if (is_dir($rootDir . '/' . $strPath . '/' . $strFile))
			{
				$arrFolders[] = $strPath . '/' . $strFile;
			}
			else
			{
				$arrFiles[] = $strPath . '/' . $strFile;
			}
		}

		// Folders
		foreach ($arrFolders as $strFolder)
		{
			$strUuid = $this->Database->getUuid();

			$this->scanUploadFolder($strFolder, $strUuid);

			$this->Database->prepare("INSERT INTO tl_files (pid, tstamp, uuid, name, type, path, hash) VALUES (?, ?, ?, ?, 'folder', ?, ?)")
						   ->execute($pid, time(), $strUuid, basename($strFolder), $strFolder, Dbafs::getFolderHash($strFolder));
		}

		// Files
		foreach ($arrFiles as $strFile)
		{
			$matches = array();

			// Handle meta.txt files
			if (preg_match('/^meta(_([a-z]{2}))?\.txt$/', basename($strFile), $matches))
			{
				$key = $matches[2] ?: 'en';
				$arrData = file($rootDir . '/' . $strFile, FILE_IGNORE_NEW_LINES);

				foreach ($arrData as $line)
				{
					list($name, $info) = explode('=', $line, 2);
					list($title, $link, $caption) = explode('|', $info);
					$arrMeta[trim($name)][$key] = array('title'=>trim($title), 'link'=>trim($link), 'caption'=>trim($caption));
				}
			}

			$objFile = new File($strFile);
			$strUuid = $this->Database->getUuid();

			$this->Database->prepare("INSERT INTO tl_files (pid, tstamp, uuid, name, type, path, extension, hash) VALUES (?, ?, ?, ?, 'file', ?, ?, ?)")
						   ->execute($pid, time(), $strUuid, basename($strFile), $strFile, $objFile->extension, $objFile->hash);

			$arrMapper[basename($strFile)] = $strUuid;
		}

		// Insert the meta data AFTER the file entries have been created
		if (!empty($arrMeta))
		{
			foreach ($arrMeta as $file=>$meta)
			{
				if (isset($arrMapper[$file]))
				{
					$this->Database->prepare("UPDATE tl_files SET meta=? WHERE uuid=?")
								   ->execute(serialize($meta), $arrMapper[$file]);
				}
			}
		}
	}
public function scanUploadFolder($strPath=null, $pid=null)
	{
		if ($strPath === null)
		{
			$strPath = Config::get('uploadPath');
		}

		$arrMeta = array();
		$arrMapper = array();
		$arrFolders = array();
		$arrFiles = array();
		$rootDir = System::getContainer()->getParameter('kernel.project_dir');
		$arrScan = scan($rootDir . '/' . $strPath);

		foreach ($arrScan as $strFile)
		{
			if (strncmp($strFile, '.', 1) === 0)
			{
				continue;
			}

			if (is_dir($rootDir . '/' . $strPath . '/' . $strFile))
			{
				$arrFolders[] = $strPath . '/' . $strFile;
			}
			else
			{
				$arrFiles[] = $strPath . '/' . $strFile;
			}
		}

		// Folders
		foreach ($arrFolders as $strFolder)
		{
			$strUuid = $this->Database->getUuid();

			$this->scanUploadFolder($strFolder, $strUuid);

			$this->Database->prepare("INSERT INTO tl_files (pid, tstamp, uuid, name, type, path, hash) VALUES (?, ?, ?, ?, 'folder', ?, ?)")
						   ->execute($pid, time(), $strUuid, basename($strFolder), $strFolder, Dbafs::getFolderHash($strFolder));
		}

		// Files
		foreach ($arrFiles as $strFile)
		{
			$matches = array();

			// Handle meta.txt files
			if (preg_match('/^meta(_([a-z]{2}))?\.txt$/', basename($strFile), $matches))
			{
				$key = $matches[2] ?: 'en';
				$arrData = file($rootDir . '/' . $strFile, FILE_IGNORE_NEW_LINES);

				foreach ($arrData as $line)
				{
					list($name, $info) = explode('=', $line, 2);
					list($title, $link, $caption) = explode('|', $info);
					$arrMeta[trim($name)][$key] = array('title'=>trim($title), 'link'=>trim($link), 'caption'=>trim($caption));
				}
			}

			$objFile = new File($strFile);
			$strUuid = $this->Database->getUuid();

			$this->Database->prepare("INSERT INTO tl_files (pid, tstamp, uuid, name, type, path, extension, hash) VALUES (?, ?, ?, ?, 'file', ?, ?, ?)")
						   ->execute($pid, time(), $strUuid, basename($strFile), $strFile, $objFile->extension, $objFile->hash);

			$arrMapper[basename($strFile)] = $strUuid;
		}

		// Insert the meta data AFTER the file entries have been created
		if (!empty($arrMeta))
		{
			foreach ($arrMeta as $file=>$meta)
			{
				if (isset($arrMapper[$file]))
				{
					$this->Database->prepare("UPDATE tl_files SET meta=? WHERE uuid=?")
								   ->execute(serialize($meta), $arrMapper[$file]);
				}
			}
		}
	}
public function scanUploadFolder ( $ strPath = null , $ pid = null ) { if ( $ strPath === null ) { $ strPath = Config :: get ( 'uploadPath' ) ; } $ arrMeta = array ( ) ; $ arrMapper = array ( ) ; $ arrFolders = array ( ) ; $ arrFiles = array ( ) ; $ rootDir = System :: getContainer ( ) -> getParameter ( 'kernel.project_dir' ) ; $ arrScan = scan ( $ rootDir . '/' . $ strPath ) ; foreach ( $ arrScan as $ strFile ) { if ( strncmp ( $ strFile , '.' , 1 ) === 0 ) { continue ; } if ( is_dir ( $ rootDir . '/' . $ strPath . '/' . $ strFile ) ) { $ arrFolders [ ] = $ strPath . '/' . $ strFile ; } else { $ arrFiles [ ] = $ strPath . '/' . $ strFile ; } } // Folders foreach ( $ arrFolders as $ strFolder ) { $ strUuid = $ this -> Database -> getUuid ( ) ; $ this -> scanUploadFolder ( $ strFolder , $ strUuid ) ; $ this -> Database -> prepare ( "INSERT INTO tl_files (pid, tstamp, uuid, name, type, path, hash) VALUES (?, ?, ?, ?, 'folder', ?, ?)" ) -> execute ( $ pid , time ( ) , $ strUuid , basename ( $ strFolder ) , $ strFolder , Dbafs :: getFolderHash ( $ strFolder ) ) ; } // Files foreach ( $ arrFiles as $ strFile ) { $ matches = array ( ) ; // Handle meta.txt files if ( preg_match ( '/^meta(_([a-z]{2}))?\.txt$/' , basename ( $ strFile ) , $ matches ) ) { $ key = $ matches [ 2 ] ? : 'en' ; $ arrData = file ( $ rootDir . '/' . $ strFile , FILE_IGNORE_NEW_LINES ) ; foreach ( $ arrData as $ line ) { list ( $ name , $ info ) = explode ( '=' , $ line , 2 ) ; list ( $ title , $ link , $ caption ) = explode ( '|' , $ info ) ; $ arrMeta [ trim ( $ name ) ] [ $ key ] = array ( 'title' => trim ( $ title ) , 'link' => trim ( $ link ) , 'caption' => trim ( $ caption ) ) ; } } $ objFile = new File ( $ strFile ) ; $ strUuid = $ this -> Database -> getUuid ( ) ; $ this -> Database -> prepare ( "INSERT INTO tl_files (pid, tstamp, uuid, name, type, path, extension, hash) VALUES (?, ?, ?, ?, 'file', ?, ?, ?)" ) -> execute ( $ pid , time ( ) , $ strUuid , basename ( $ strFile ) , $ strFile , $ objFile -> extension , $ objFile -> hash ) ; $ arrMapper [ basename ( $ strFile ) ] = $ strUuid ; } // Insert the meta data AFTER the file entries have been created if ( ! empty ( $ arrMeta ) ) { foreach ( $ arrMeta as $ file => $ meta ) { if ( isset ( $ arrMapper [ $ file ] ) ) { $ this -> Database -> prepare ( "UPDATE tl_files SET meta=? WHERE uuid=?" ) -> execute ( serialize ( $ meta ) , $ arrMapper [ $ file ] ) ; } } } }
Scan the upload folder and create the database entries

@param string  $strPath The target folder
@param integer $pid     The parent ID
Scan the upload folder and create the database entries