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 8377325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:38:39+00:00 2026-06-09T15:38:39+00:00

If a user was requesting a file on a service, I could normally protect

  • 0

If a user was requesting a file on a service, I could normally protect them from accessing documents outside the scope of what I want by using PHP’s realpath() and ensuring it is under the root directory. Such as like this:

$path = realpath($_GET['path']);
// Protect against LFI vulnerabilities
if (substr($path, 0, strlen($root)) == $root)
{
    // safe
}

However, realpath() only works on files that already exist.
What if I want to ensure that the location the user is about to have my script write to is under the root?

I can’t use realpath(), should I just check and strip out ‘..’ references?
Or is there a better way?

  • 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-06-09T15:38:41+00:00Added an answer on June 9, 2026 at 3:38 pm

    How about checking if realpath(dirname($file)) is under the root directory?

    $dir = $path;
    $found = false;
    
    while ($dir) {
        $dir = dirname($dir);
        if (!is_dir($dir)) {
            continue;
        }
    
        if (strpos(realpath($dir), $root) === 0) {
            $found = true;
        }
    
        break;
    }
    
    var_dump($found);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script, requesting a file from user using FileChooserDialog. When done,
I want to download a file in flex. Here is my desired flow User
I want to redirect clients requesting the file '/File.mp3' to a PHP script that
How i can avoid cross-site-scripting, by knowing, from what site user is requesting data?
I have a method that returns a PDF file using DOMPDF . It sends
I'm trying to stream an audio file from a server protected by session-based authentication,
1. HTML I have an input requesting the user to enter their PIN code:
I need to invoke a ColdFusion function(present in a .cfm file) when the user
I'm currently using a homegrown method to run a process as a different user
I want to build an application that will allow the user to upload a

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.