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

The Archive Base Latest Questions

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

The Problem This is a very simple-to-understand question. I’m having a user submit a

  • 0

The Problem

This is a very simple-to-understand question.

I’m having a user submit a URL, for example “http://example.com/path/filename.html”.

I’m using PHP’s dirname() function to get the so-called “base” of this URL. For the above example, that would be “http://example.com/path”.

My problem arises when the user enters this:

http://example.com/blog

If you type the above into your browser, you will see the index.php or .html page in the folder called “blog”. However, PHP’s dirname() will return only “http://example.com”.

I’m not sure if it thinks that “blog” is an extension-less file, if that exists, but I can’t really find a solution.

Things I’ve Tried

I first tried getting the extension of the URL using this quick method:

$url = 'http://example.com/index.php';
$file_extension = end(explode('.', $filename));

Then, I would check if the extension existed using PHP empty(). If the extension exists, that means that a filename was entered after the folder, such as “http://example.com/path/file.html”, and dirname() is perfect. If the extension doesn’t exist, no file was entered and the last item in the path is a folder, so it is already “the base”.

However, in the case of simply “http://example.com/path/”, the above would return “.com/path/” as the file extension, which we all know doesn’t exist. In this case, I would use the dirname() function and cut off “/path/”.

EDIT:

Taking the extension of basename($url) won’t work because if the user enters “http://example.com” basename() returns “example.com”, the extension for which is supposedly
“.com”

Hopefully, someone has had the same problem and knows the solution. I’m still looking, but any answers are wholly appreciated!!

  • 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-09T12:18:39+00:00Added an answer on June 9, 2026 at 12:18 pm

    EDIT
    Ok, last time before I give up:

    function getPath($url){
        $parts=explode("/",$url);
        $patharray=array(".","http:","https:");
        if(!in_array(pathinfo($url,PATHINFO_DIRNAME),$patharray) && strpos($parts[count($parts)-1], ".")!==false)
            unset($parts[count($parts)-1]);
        $url=implode("/",$parts);
        if(substr($url,-1)!='/')
            $url.="/";
        return $url;
    }
    echo getPath("http://www.google.com/blog/testing.php")."\n";
    echo getPath("www.google.com/blog/testing.php")."\n";
    echo getPath("http://www.google.com/blog/")."\n";
    echo getPath("http://www.google.com/blog")."\n";
    echo getPath("http://www.google.com")."\n";
    echo getPath("http://www.google.com/")."\n";
    echo getPath("www.google.com/")."\n";
    echo getPath("www.google.com")."\n";
    

    Any url with the last portion having a “.” in it is parsed out, otherwise it is left alone. It uses pathinfo() to check to see if it is just a domain (“google.com” or “http://www.google.com”) and then leaves the last portion alone as there would be a “.” in it.
    Here is the script output:

    http://www.google.com/blog/
    www.google.com/blog/
    http://www.google.com/blog/
    http://www.google.com/blog/
    http://www.google.com/
    http://www.google.com/
    www.google.com/
    www.google.com/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Its very simple question, I'm new to this and I can't understand the problem
This problem seems very simple to me, but I've been unable to fix it,
This is a very simple code in place of a bigger problem, but I'm
Problem : This is a probably a very basic question, but how do I
The very act of asking this question suggests that my approach to this problem
Hi this question or problem i have its very hard i have search and
It seems I don't understand something simple about Protocol Buffers, but this is very
I think this has to be a very simple question but I am not
Possible Duplicate: Can main function call itself in C++? I found this problem very
This problem is very strange and I'm hoping someone can help me. For the

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.