Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display image with wildcard in src

Tags:

html

I have a series of photos on a server with a strict naming convention: "uniqueId-readableName.jpg". I do this because my website database currently only logs the uniqueID (and some unrelated info), but people occasionally need to look at the file server directly to browse the photos (via FTP) so a readable name is useful. For example

001456-War Horse.jpg
003295-Sunshine Daiseys.jpg
129084-Laboring at the farm 2013-08-11.jpg

Now, I'm fairly sure the best option would be to set up the database with a record of the full file names, but I just wanted to see if anyone had any ideas I may have missed. This question on SO is similar, but here I have strict naming conventions - not sure if that opens up any possibilities or not.

I'm applying this to img, but the same idea could be appled to any file extension (eg, download "789-My Homework.zip" or "123-Family vacation.zip").

As an example of what I'm looking for, in Windows Explorer you can do a file search for

0*.jpg

and all of the following files can be returned

001456-War Horse.jpg
003295-Sunshine Daiseys.jpg
029084-Laboring at the farm 2013-08-11.jpg

In my case the beginning part is always unique, so I'd like to use something like 001456-*.jpg and have it return 001456-War Horse.jpg.

Is there any way do this on a website?

<img src="001456-*.jpg" />
like image 932
DACrosby Avatar asked Aug 26 '13 04:08

DACrosby


1 Answers

Although no such method exists, you can still do a server side scripting to acheive the functionality you require.

For example in PHP you could use in-built commands to browse a folder, select all files matching the criteria name as '001456-*.jpg' and then depending upon the number of records returned select the first filename and insert it into an IMG tag.

Here is a sample implementation in PHP:

$files = array();
$id = "001456";
$files = glob("id-*.jpg");

echo "<img src='$files[0]' />"; //Assuming one file is found for every unique ID.
like image 60
Sunny R Gupta Avatar answered Sep 27 '22 21:09

Sunny R Gupta