Allowing Variable Image Extensions (Glob?)

Whether it is only theoretical or not can only answer the OP. It is enough if he wants some icons to be animated gifs and your assumption is already wrong. Compression type is also important based on the content.

Yes, you do need a very busy site and I mean very, very busy. I’ve used this technique a few times and the performance impact is almost zero. However:

That is correct, for a directory with a lot of files this is not a good solution - I don’t know the details here but if he kept each icon in a separate directory then there is only one file. For large directories I would simply store the filename with the extension in the database (or any other data store) and the problem would be solved. I think this would be the best solution in this case - if the OP gets the image ID from somewhere then he could also store the image filename in that same somewhere.

There is also a completely different solution to the problem without any PHP with the use of Apache mod_rewrite in .htaccess:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/images/
RewriteCond %{REQUEST_FILENAME}.jpg -f
RewriteRule (.*) $1.jpg [L]

RewriteCond %{REQUEST_URI} ^/images/
RewriteCond %{REQUEST_FILENAME}.gif -f
RewriteRule (.*) $1.gif [L]

RewriteCond %{REQUEST_URI} ^/images/
RewriteCond %{REQUEST_FILENAME}.png -f
RewriteRule (.*) $1.png [L]

images is the name of the directory (as appears in the URL after the domain name) where images are kept. Then in the <img> tag you reference the image file without the extension:

<img src="images/my-icon" alt="">

The rewrite rule successively checks if a file exists with the extensions .jpg, .gif and .png and if it finds one then it is sent to the browser. These checks are only done for URL paths beginning with images/.

If mod_rewrite is available on the server (it often is) then it’s a pretty simple solution and will perform well even if there are many files in the directory (again - you would need an extremely busy site for this technique to be too slow). And the advantage is the server sends the correct mime-type headers depending on the file extension and the images can be kept on the server with their proper file extensions.

2 Likes

Wow, thanks for that script; this is getting more educational by the minute. :wink:

OK, I opened the image in a new tab, right-clicked on it and copied the following URL:

http://gs/images/sections/world/top/flower/usa/ca.jpg

I then modified your script accordingly…

echo '
 getcwd() === ' .getcwd();
   $fileExt           = '*.*';
   $globDirectoryPath = 'http://gs/images/sections/world/top/flower/usa/ca.jpg'; //  .'i*.php';
   if( is_dir( $globDirectoryPath ) ):
      $aFiles  = glob( $globDirectoryPath .$fileExt );
      echo ''; print_r($aFiles); echo '
';
   else:
      echo 'Problem: this is NOT a valid path: ' .$globDirectoryPath;
      die;
   endif;

When I preview the page, it says…

getcwd() === /Applications/MAMP/htdocs/gs/world

Problem: this is NOT a valid path: http://gs/images/sections/world/top/flower/usa/ca.jpg

When I change the $globDirectoryPath to either of the following…

$globDirectoryPath = '<img src="http://gs/images/sections/world/top/flower/usa/ca.jpg">';

or…

$globDirectoryPath = '<img src="/images/sections/world/top/flower/usa/ca.jpg">';

I get the same message, even though the image displays just after the message. I also get the same message if I change the glob path to this, with or without the leading slash:

$globDirectoryPath = '/Applications/MAMP/htdocs/gs/world/images/sections/world/top/flower/usa/ca.jpg';

Sorry, I replied to John_Betong before I saw all the newer comments on this thread.

Your suggestion about simply storing file names in a database is a good one. In fact, I just recently started adding some image information to my database - permissions, captions, etc. So it would be easy to add another column featuring image names.

I’ll check out your $foundImageFilename suggestion, too, however, as I have similar projects that I can’t use a database for.

(Wow, who could imagine that a function with a name like “glob” could be so confusing? There must be others who have had a hard time figuring out how it works - I hope. ;))

It’s a little ridiculous how difficult this being made out to be. All you really need is a if/else. If the variant (png) exists use it if it doesn’t use jpg. That’s it, no glob or anything else. 4 lines of code. A conditional with file_exists().

Yes, I was thinking along similar lines earlier…

“I can always just make a PHP switch and code in the exceptions manually.”

But it is nice if you can automate things as much as possible when you’re working with hundreds or thousands of files.

If the only thing that changes is the extension all you need is a conditional. On Windows you need to be using Windows compatible paths. However, those same paths won’t be compatible with Linux. Xamp has a large amount of inconsistencies with Linux. Something that works on xamp might not work on a Linux server and vice versa. This is normally the point I would tell you to use a vm but I recently got reported for telling people how to improve their work flow so what ever.

By “conditional,” you mean an IF/ELSE script, right?

I upgraded to a Mac years ago, so I no longer have to sweat all the Windows stuff. Windows > Mac, FrontPage > Dreamweaver and Microsoft Server > Linux.

Workflow is very important. I do somethings a little backwards to suite my workflow, but it’s worth it in the long run. For example, spreadsheets are SO convenient for organizing data. So I sometimes have to modify my database tables a bit to match to my spreadsheets.

I don’t understand why you’re recommending a VM, though - were you assuming I’m using Windows?

I could have swore I saw something about xamp in this thread. Anyway yes a if/else with file exists to check whether a png exists and if not use the jpg.

I’m using MAMP, though I don’t recall mentioning it. I think I may have used XAMP long ago.

My mistake

Can you try the following $globDirectoryPath

# Try My Best Guess 
    $globDirectoryPath = '/Applications/MAMP/htdocs/gs/images/sections/world/top/flower/usa/ca.jpg

# Sledge Hammer Approach and "DRILL DOWN"
    $globDirectoryPath = '/Applications/*.*
    $globDirectoryPath = '/Applications/MAMP/*.*
    $globDirectoryPath = '/Applications/MAMP/htdocs/*.*
    $globDirectoryPath = '/Applications/MAMP/htdocs/gs/*.*
    $globDirectoryPath = '/Applications/MAMP/htdocs/gs/images/*.*
    $globDirectoryPath = '/Applications/MAMP/htdocs/gs/images/sections/*.*
    $globDirectoryPath = '/Applications/MAMP/htdocs/gs/images/sections/world/*.*
    $globDirectoryPath = '/Applications/MAMP/htdocs/gs/images/sections/world/top/*.*
    $globDirectoryPath = '/Applications/MAMP/htdocs/gs/images/sections/world/top/flower/*.*
    $globDirectoryPath = '/Applications/MAMP/htdocs/gs/images/sections/world/top/flower/*.*
    $globDirectoryPath = '/Applications/MAMP/htdocs/gs/images/sections/world/top/flower/usa/*.*
    $globDirectoryPath = '/Applications/MAMP/htdocs/gs/images/sections/world/top/flower/usa/cs*.*
    $globDirectoryPath = '/Applications/MAMP/htdocs/gs/images/sections/world/top/flower/usa/cs*.jpg

# Your Results 
    # IMG URL
    http://gs/images/sections/world/top/flower/usa/ca.jpg

    # PATH Current
    getcwd() === /Applications/MAMP/htdocs/gs/world



I haven’t drilled down through all of those yet, but I tried the last line first…

$globDirectoryPath = '/Applications/MAMP/htdocs/gs/images/sections/world/top/flower/usa/cs*.jpg';
	echo $globDirectoryPath;
echo '<br> getcwd() === ' .getcwd();

And it displays this:

getcwd() === /Applications/MAMP/htdocs/gs/world

But I just discovered the stupid mistake that (I think) has been fouling me up all along. I have my .htaccess file set up so that it only allows URL’s with lower case letters. So that might explain why URL’s with Applications/MAMP aren’t working.

Sorry I didn’t think of that earlier.

1 Like

Using XAMPP doesn’t necessarily mean Windows - it has Linux and Mac versions as well as the Windows one.

(note also it has two Ps as it supports PHP as well as Perl).

So it seems the guy is going to do up to a hundred reads or even more or every page load.

This is what you get when you let someone to do things their own way, instead of forcing them to do the right way.

Inexperience breeds incompetence. It is the same for any industry. Which I guess is why some people ask questions here. Not to mention sometimes the poor decisions of others are difficult / not worth correcting.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.