add command:rm;optimize cd

This commit is contained in:
2024-07-15 08:46:15 +08:00
parent 94a5dadbec
commit 0279ae6493
5 changed files with 71 additions and 17 deletions
+26
View File
@@ -0,0 +1,26 @@
<?php
function cd($commands)
{
if(sizeof($commands)>2){
file_put_contents("recent.txt", "Too many arguments.<br/>", FILE_APPEND);
return;
}
$pwd = file_get_contents("pwd.txt");
if ($commands[1] == ".." || $commands[1] == NULL) {
$pwd = substr($pwd, 0, strrpos($pwd, "/"));
file_put_contents("pwd.txt", $pwd);
} else if (str_starts_with($commands[1], "/")) {
file_put_contents("pwd.txt", $commands[1]);
} else {
if (opendir("fileroot" . $pwd . "/" . $commands[1])) {
if ($pwd == "/")
file_put_contents("pwd.txt", $pwd . $commands[1]);
else {
file_put_contents("pwd.txt", $pwd . "/" . $commands[1]);
}
} else {
file_put_contents("recent.txt", "Directory does not exist.<br/>", FILE_APPEND);
}
}
}
?>
+35
View File
@@ -0,0 +1,35 @@
<?php
function deleteDirectory($dir)
{
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? deleteDirectory("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
function rm($commands)
{
$pwd = file_get_contents("pwd.txt");
if ($commands[1] == "-r") {
if (is_dir("fileroot" . $pwd . "/" . $commands[2])) {
if (deleteDirectory("fileroot" . $pwd . "/" . $commands[2])) {
file_put_contents("recent.txt", "Directory deleted.<br/>", FILE_APPEND);
} else {
file_put_contents("recent.txt", "Error deleting.<br/>", FILE_APPEND);
}
} else {
file_put_contents("recent.txt", "Not a directory.<br/>", FILE_APPEND);
}
} else {
if (is_dir("fileroot" . $pwd . "/" . $commands[1])) {
file_put_contents("recent.txt", "Is a directory.<br/>Try rm -r<br/>", FILE_APPEND);
} else {
if (unlink("fileroot" . $pwd . "/" . $commands[1])) {
file_put_contents("recent.txt", "File deleted.<br/>", FILE_APPEND);
} else {
file_put_contents("recent.txt", "Error deleting.<br/>", FILE_APPEND);
}
}
}
}
?>