Jump to content

Getting file name from the url


tsdesai7

Recommended Posts

Hello,

 

I am trying to get the filename extension from the URL. It works fine if the URL doesn't have parameters after the file name . Below is my code so far

 

$ext = parse_url($url);
$pth=$ext['path'];
$filename = pathinfo( parse_url( $url, PHP_URL_PATH ), PATHINFO_EXTENSION ); 
 
when i echo filename it just prints ch but i need to know the extension of filename like whether its php or .p or .js etc.
 
I would really appreciate any help on this.
Many Thanks,
Teju
Link to comment
Share on other sites

I'm not sure you understand how paths work. In the path

/test/test.php/ch

the filename is "ch", and there's no extension. If you're interested in the "test.php", that's not the filename. It's technically a directory.

 

It may be the case that your webserver accepts fantasy paths and transforms them to different paths. I think CGI had an obscure feature which allowed this. But you won't be able to process those fantasy paths with standard file functions.

Link to comment
Share on other sites

I think your only option is to create your own function to get the piece of data you need. This requires that you "know" what can and cannot exist in the URL. For example, if you know that a period cannot exist in the non-file elements you could determine it pretty simply. For example, is this something that would exist in your data:

 

https://test.com/test/test.php/c.h

 

If so, then perhaps you need logic to look for specific extensions. But,t hen you have to know all the possible extensions.

 

Building a customer function would not be difficult, but you have to know what is and is not possible in the values to be parsed so you can create the appropriate logic.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.