Jump to content

PHP not getting right URL

DomProNet
<?php
// Retrieve the selected files from the POST request
$selectedFiles = $_POST['files'];
$folderName = $_POST['folder'];

// Extract the folder name from the URL hash if it exists
if (strpos($folderName, '#') !== false) {
    $folderName = substr($folderName, strpos($folderName, '#') + 1);
}

// Decode the folder name if it is URL-encoded
$folderName = urldecode($folderName);

// Replace "%2F" with "/"
$folderName = str_replace('%2F', '/', $folderName);

// Function to recursively delete a directory and its contents
function deleteDirectory($dir) {
    if (!file_exists($dir)) {
        return;
    }

    $files = array_diff(scandir($dir), array('.', '..'));
    foreach ($files as $file) {
        $filePath = $dir . '/' . $file;
        if (is_dir($filePath)) {
            deleteDirectory($filePath);
        } else {
            unlink($filePath);
        }
    }

    rmdir($dir);
}

// Check if the folder name is empty (root directory of 'your_files')
if (empty($folderName)) {
    $directoryPath = 'your_files';
} else {
    // Remove the file name from the folder path, if provided
    $folderPathParts = explode('/', $folderName);
    array_pop($folderPathParts);
    $folderName = implode('/', $folderPathParts);

    // Construct the full directory path based on the folder name
    $basePath = dirname($_SERVER['SCRIPT_FILENAME']); // Get the base path of the PHP file
    $directoryPath = $basePath . '/your_files/' . $folderName;
}

// Perform the deletion operation for each selected file
foreach ($selectedFiles as $file) {
    $filePath = $directoryPath . '/' . $file;

    if (is_dir($filePath)) {
        deleteDirectory($filePath);
    } else {
        unlink($filePath);
    }
}

// Send a response back to the JavaScript code
$response = array(
    'message' => 'Files deleted successfully.'
);

// Encode the response as JSON and send it back
echo json_encode($response);
?>

This code can delete stuff from dashboard.php# which is the your_files directory. dashboard.php#foldername also works but dashboard.php#foldername%2Fsubfolder doesn't work. I don't know what I'm doing wrong, can someone help? Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

  • 4 weeks later...

"array_pop($folderPathParts);" removes the last element of the array, whether it's a file or a folder, is seems to me that you should check if the last item in the array is a filename before popping it. This line now removes the subfolder.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now