Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

$file_extension - Any benefit on getting it from the file?


piernas

Recommended Posts

Several modules retrieve .php file extension from themselves this way for later use:

$file_extension = substr($PHP_SELF, strrpos($PHP_SELF, '.'));

Is there any benefit on doing that instead of

$file_extension = '.php';

I cannot see why it is needed. Any idea?

Link to comment
Share on other sites

The first method gets the type for any file. The second only works for php files. If you are 100% sure the file checked will be a php file then the second can be used. But if the file isn't a php file and you use the second method, it will cause a failure in the code.

Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Get the latest versions of my addons

Recommended SEO Addons

Link to comment
Share on other sites

Add a page named list.html and in that file add the following, or whatever the command is.

<?php echo product_listing.php; ?>

The command $file_extension = '.php'; will fail because that is not the correct type. Something like that may never happen but that is what I meant about knowing for sure.

Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Get the latest versions of my addons

Recommended SEO Addons

Link to comment
Share on other sites

The way I've seen it used in code is:

 

You want to get code from modules.

  • get extension from currently loaded page i.e. modules_content.php -> you get '.php'
  • Iterate through all files of a directory
  • compare if the file has the same extension than our currently loaded page (modules_content.php) with substr($PHP_SELF, strrpos($PHP_SELF, '.'))
  • it it is a '.php' file then do something...

I don't see the intention of getting the currently loaded extension just to list php files in a directory that has nothing to do with this file, that's why I was asking :rolleyes:

Link to comment
Share on other sites

Without knowing the actual code I can only guess at what it is for but, typically, such code is used to make it more generic. Let's say you have a module that uses that code and there is a directory with one php file and 500 image, xml, html or whatever files. Would you rather check the one file or check 501 files? Changing the extension to be specific would also work - in that case. But if you duplicated that module for some new addon you were creating and you wanted the module to work with html files instead of php files, your code would fail because it would only work with php files. While the generic code would work because it determines the type of file from the page it is on. 

Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Get the latest versions of my addons

Recommended SEO Addons

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...