insert folder structure to mysql

Different kind of file browsers

insert folder structure to mysql

Postby blodan on 19 Dec 2007, 01:29

This script simply inputs all folder names/lastchanged and file names/size/lastchanged into a mysql database, warning takes alot of time on folders with much files and subfolders.

To output the data you have gathered from this script in a nice browser you have to read this topic where i have posted my browser script

  1. CREATE TABLE IF NOT EXISTS `files` (
  2.   `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
  3.   `folderid` BIGINT(20) NOT NULL,
  4.   `name` TEXT NOT NULL,
  5.   `size` BIGINT(20) NOT NULL,
  6.   `lastchanged` INT(11) NOT NULL,
  7.   PRIMARY KEY (`id`)
  8. ) ENGINE=INNODB  DEFAULT CHARSET=latin1;
  9.  
  10. CREATE TABLE IF NOT EXISTS `folders` (
  11.   `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
  12.   `name` TEXT NOT NULL,
  13.   `subfolder` BIGINT(20) NOT NULL,
  14.   `lastchanged` INT(11) NOT NULL,
  15.   PRIMARY KEY (`id`)
  16. ) ENGINE=INNODB  DEFAULT CHARSET=latin1;


  1. <?
  2. $connecta = mysql_connect("host", "user", "password");
  3. mysql_select_db("database");
  4. $dir = "/jail/glftpd/site/";
  5.  
  6. function rd($dirr,$folderID) {
  7.         if (is_dir($dirr)) {
  8.                 if ($dh = opendir($dirr)) {
  9.                         while (($file = readdir($dh)) !== false) {
  10.                                 if($file != ".." && $file != ".") {
  11.                                         if(filetype($dirr . $file) == "dir") {
  12.                                                 echo "Folder: ".$dirr.$file."/\r\n";
  13.                                                 mysql_query("insert into folders (name, subfolder, lastchanged) Values('".mysql_real_escape_string($file)."', '".$folderID."', '".mysql_real_escape_string(filemtime($dirr.$file))."')");
  14.                                                 rd($dirr.$file."/", mysql_insert_id())."\r\n";
  15.                                         }else{
  16.                                                 echo "Filename: $file : filetype: " . filetype($dirr . $file) . "\r\n";
  17.                                                 mysql_query("insert into files (name, folderid, size, lastchanged) Values('".mysql_real_escape_string($file)."', '".$folderID."', '".mysql_real_escape_string(filesize($dirr.$file))."', '".mysql_real_escape_string(filemtime($dirr.$file))."')");
  18.                                         }
  19.                                 }
  20.                         }
  21.                         closedir($dh);
  22.                 }
  23.         }
  24. }
  25.  
  26. if (is_dir($dir)) {
  27.         rd($dir,0);
  28. }
  29. mysql_close($connecta);
  30. ?>
User avatar
blodan
Site Admin
 
Posts: 27
Joined: 17 Nov 2007, 00:16

Re: insert folder structure to mysql

Postby butlins12 on 01 Feb 2008, 01:29

This looks like it could be the answer to my head-scratching around a simple document management system for an extranet I'm doing - thanks. Just one thing - line 4 which sets the directory, is it based from the root of the web site, or the root of the FTP site?
butlins12
 
Posts: 2
Joined: 01 Feb 2008, 01:24

Re: insert folder structure to mysql

Postby blodan on 01 Feb 2008, 16:08

butlins12 wrote:This looks like it could be the answer to my head-scratching around a simple document management system for an extranet I'm doing - thanks. Just one thing - line 4 which sets the directory, is it based from the root of the web site, or the root of the FTP site?


Its the root from wherever its going to start reading folders/files, in this case its the root of the ftp site. I´ve only tried using this code with php-cli so i havent tried running it trough a webserver.

To display them you simply have to use the viewing script in the other thread thats linked to in this thread ( viewtopic.php?f=7&t=16&p=23 )
User avatar
blodan
Site Admin
 
Posts: 27
Joined: 17 Nov 2007, 00:16

Re: insert folder structure to mysql

Postby butlins12 on 01 Feb 2008, 19:40

If you save the following:
  1. <?php
  2. $p = getcwd();
  3. echo $p;
  4. ?>

as, say 'path.php' in the folder that your documents are saved in, and then browse to that file, then it'll return the absolute path that you need to enter as the variable in line 4. Then you can run the insert folder structure php by browsing to it. This is a great bit of script to start with - thanks a lot. I'll just need to work through it a bit so that it doesn't create duplicates if it's run more than once one the same folder - it basically just needs a trap somewhere in it so that each record is unique.
butlins12
 
Posts: 2
Joined: 01 Feb 2008, 01:24

Re: insert folder structure to mysql

Postby Brumer on 18 May 2009, 15:19

Hi,

can anyone do it in reverse?
Brumer
 
Posts: 1
Joined: 18 May 2009, 15:10


Return to File browsers

Who is online

Users browsing this forum: No registered users and 1 guest

cron


  © 2007-2009 PRAXXA HB, Org. nr 969727-7227 Driven by Daniel Ylitalo & Daniel Lindholm, info@praxxa.com  
  The PRAXXA Network » www.praxxa.com » www.phpBNC.se » www.phpguides.eu » www.phpstuff.org » Nyheter