mirror of
https://github.com/sigmasternchen/webcli
synced 2025-03-15 06:08:54 +00:00
filemanager - almost complete
This commit is contained in:
parent
5bcd21009c
commit
9c49592378
1 changed files with 65 additions and 0 deletions
65
backend/fileManager.php
Normal file
65
backend/fileManager.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
require_once("mysqlConnect.php");
|
||||
|
||||
class fileManager {
|
||||
static public function getIdByPath ($path) {
|
||||
global $db;
|
||||
$path = fileManager::cleanPath($path);
|
||||
if (strpos($path, "/") === 0)
|
||||
$path = substr($path, 1);
|
||||
$array = explode("/", $path);
|
||||
$parent = 0;
|
||||
for($i = 0; $i < count($array) - 1; $i++) {
|
||||
$result = $db->query("SELECT `files`.`ID` AS `ID` FROM `files` INNER JOIN `fileTypes` ON `files`.`fileTypeFK`=`fileTypes`.`ID` WHERE `files`.`parentFK`=" . $parent . " AND `fileTypes`.`name`='directory' AND `files`.`name`='" . $db->real_escape_string($array[$i]) . "'");
|
||||
if (!$result->num_rows)
|
||||
throw new Exception("no such file or directory");
|
||||
$parent = $result->fetch_object()->ID;
|
||||
}
|
||||
$result = $db->query("SELECT `ID` FROM `files` WHERE `parentFK`=" . $parent . " AND `name`='" . $db->real_escape_string($array[count($array) - 1]) . "'");
|
||||
if (!$result->num_rows)
|
||||
throw new Exception("no such file or directory");
|
||||
return $result->fetch_object()->ID;
|
||||
}
|
||||
static public function cleanPath($path) {
|
||||
|
||||
while (strpos($path, "//") !== false) {
|
||||
$path = str_replace("//", "/", $path);
|
||||
}
|
||||
|
||||
$array = explode("/", $path);
|
||||
for($i = 0; $i < count($array); $i++) {
|
||||
$value = $array[$i];
|
||||
if ($value == "")
|
||||
array_splice($array, $i--, 1);
|
||||
if ($value == ".")
|
||||
array_splice($array, $i--, 1);
|
||||
if ($value == "..") {
|
||||
if ($i == 0)
|
||||
throw new Exception("invalid path");
|
||||
array_splice($array, $i - 1, 2);
|
||||
$i = $i - 2;
|
||||
}
|
||||
}
|
||||
return implode("/", $array);
|
||||
}
|
||||
static public function getFileById($ID) {
|
||||
global $db;
|
||||
$result = $db->query("SELECT * FROM `files` WHERE `ID`=" . $ID);
|
||||
if (!$result->num_rows)
|
||||
throw new Exception("no such file or directory");
|
||||
return $result->fetch_object();
|
||||
}
|
||||
static public function getPathById($id) {
|
||||
global $db;
|
||||
$array = array();
|
||||
while ($id) {
|
||||
$result = $db->query("SELECT `name`, `parentFK` FROM `files` WHERE `ID`=" . $id);
|
||||
$result = $result->fetch_object();
|
||||
$array[] = $result->name;
|
||||
$id = $result->parentFK;
|
||||
}
|
||||
$array = array_reverse($array);
|
||||
return "/" . implode("/", $array);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Reference in a new issue