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

  • Home
  • SEARCH
  • 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 7920181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:07:09+00:00 2026-06-03T16:07:09+00:00

I have a question if I may. I have a variable named $source and

  • 0

I have a question if I may. I have a variable named $source and this should contain a relative path to a file, for example ./uploads/2012/some-document.pdf

Now, this $source variable will contain user input, via $_GET and $_POST. I don’t want people to enter URLs and I only want to do something if the file exists only on the local server.

My question is what is the best way to check if a file exists on the local server only?


This is what I’ve got so far:

1) file_exists may return true depending on the server configuration, so I could use this alongside stripos to check if the first few charatcers of the string is http:// like so:

if( file_exists($source) && stripos($source,'http://')!==0 ) {
    echo 'File exists on local';
}

However, the downside would be I’d have to specify all the different URL types such as https://, http:// and ftp:// just to be safe.

2) I use realpath to get the absolute path of the file, and this returns false if it cannot be found. This seems to be pretty solid, but not 100% this is the best application for it

3) Use preg_replace to remove all URL mentions in the string first, and then simply use file_exists. Whilst this would probably be the most secure, it would most likely be the most intensive and I’d prefer not to use this method.

  • 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-03T16:07:10+00:00Added an answer on June 3, 2026 at 4:07 pm

    In addition to the other answers, you can deny paths that use a scheme simply with:

    if (parse_url($path, PHP_URL_SCHEME)) {
        // has something:// so ignore
    }
    

    This would also work with specialty wrappers such as php://, zlib://, etc…

    You can also force a local check on the input variable by prepending with file://:

    file_exists('file://' . $path);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question may have been asked before, but I had trouble finding an answer,
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
This question may not directly relate to programming. I have noticed that the technology
I'm new to C++, so this question may be basic: I have two classes
May it's very simple question, but I'm stuck here. I have variable val as
This may be a bit of an odd question, and what I have in
I have a question that may be quite naive, but I feel the need
I have a question which may be somewhat silly because I'm pretty sure I
I have a question that may sound odd, but being somewhat of a newbie,
My question may be stupid but I have a 2 days experience in MYSQL.

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.