tsdesai7 Posted June 28, 2017 Share Posted June 28, 2017 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 $url="https://test.com/test/test.php/ch $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 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 28, 2017 Share Posted June 28, 2017 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. Quote Link to comment Share on other sites More sharing options...
tsdesai7 Posted June 28, 2017 Author Share Posted June 28, 2017 Hi Guru, Its the CGI that i am having problems as you have spotted and is driving me insane to work around this problem. The other files like php etc are fine. Thanks Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 28, 2017 Share Posted June 28, 2017 What are you trying to achieve? Is this somebody else's server? What do you need the extension for? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 28, 2017 Share Posted June 28, 2017 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. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.