Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6237267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:59:49+00:00 2026-05-24T10:59:49+00:00

Hi I’m making some kind of file explorer on PHP and I’d like for

  • 0

Hi I’m making some kind of file explorer on PHP and I’d like for example if I have a root folder like this:

/ (root)
|
|
|---home
      |
      |
      |---user
           |
           |
           |---folder
           |
           |---folder1
           |
           |---file1

Now how I would do so user would be only able to browse files inside /home/user/ and would not be able to do for example cd ../../ and go outside? Thanks a lot 🙂
This is the code I use for the cdcommand:

<?php
function cd($actualdir, $dir) {
    //echo $actualdir.$dir."<br>";
    if($dir == "..") {
        $expdir = explode("/", $actualdir);
        $newdir = array_pop($expdir);
        $fulldir = "";
        foreach($expdir as $value) {
            $fulldir .= $value."/";
        }
        $fulldir = substr($fulldir, 0, -1);
        if($fulldir == "./rootfs/home") {
            return "permission denied";
        } else {
            return $fulldir;

        }

    } else {
        if(file_exists($actualdir."/".$dir)) {
            //echo $dir;
            return $actualdir."/".$dir;
        } else {
            return "no";
        }
    }

}

?>

BTW The root dir is just a directory on the web server root that should act as the system root for the shell

EDIT: Basically, if the user is user only allow him to enter to /home/user and its subdirectories. If the user is testonly to /home/test and subdirectories. That’s all

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:59:50+00:00Added an answer on May 24, 2026 at 10:59 am

    You could use realpath($fulldir) to let the filesystem resolve relative paths (like .. and symbolic links).

    $fulldir = $actualdir . "/" . $dir;
    $rootdir = "/home/user";
    $length = strlen($rootdir);
    
    $realdir = realpath($fulldir);
    if ($realdir === false)
      return "file does not exist";
    
    if(substr($realdir, 0, $length) != $rootdir)
      return "permission denied";
    
    return "OK: $realdir";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I would like to count the length of a string with PHP. The string
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.