tipsun
Off
(19)
Moderator
2013.02.01 10:10
Можно 1 циклом обойтись, в функцию подготовленный prepare запихать и вперед.
- - - -
Надо еще с путями разобраться и parent_id
CREATE TABLE IF NOT EXISTS `downloads` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`path` text NOT NULL,
`name` varchar(50) NOT NULL,
`parent_id` bigint(20) NOT NULL,
`type` varchar(20) NOT NULL,
`size` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`,`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Код:
span style="color: #0000BB"><?phpfunction dread($parent) { $array = array(); if (false !== ($resource = opendir($parent))) { while (false !== ($name = readdir($resource))) { if ('.' != $name && '..' != $name) { $child = $parent . DIRECTORY_SEPARATOR . $name; $array[] = $child; if (is_dir($child)) { $array = array_merge($array, dread($child)); } } } closedir($resource); } return $array;}// Get physical pathes$root = realpath('folders');$physical = dread($root);// Get virtual pathes$virtual = array();$pdo = new Pdo('mysql:dbname=downloads;host=127.0.0.1;', 'root', '');$qt = 'SELECT `path` FROM `downloads`;';if (false !== ($q = $pdo->query($qt))) { if ($q->rowCount()) { $virtual = $q->fetchAll(PDO::FETCH_COLUMN); }}// Search new and invalid pathes$new = array_diff($physical, $virtual);// $invalid = array_diff($virtual, $physical);// Add new pathes in virtual$qt = 'INSERT INTO `downloads` (`path`, `name`, `type`, `parent_id`, `size`) ' . 'VALUES (:path, :name, :type, :parent_id, :size)';$pdo->beginTransaction();$q = $pdo->prepare($qt);foreach ((array) dread($root) as $path) { $q->execute(array( ':path' => $path, ':name' => basename($path), ':type' => ($type = filetype($path)), ':parent_id' => 0, ':size' => ('file' == $type ? filesize($path) : 0) ));}$pdo->commit(); |
Отредактировано tipsun (2013.02.01 12:12)