admin
Guest
|
 |
« on: July 08, 2009, 09:03: PM » |
|
if you're storing a uploaded files name on a mysql table field the easiest way to get the file extention is to use a variable to pull it out. the variable can then be used if need for a show if display;
we had a job where the client wanted to be able to upload images and display them and one of the image or file formats they wanted to use was swf - displaying swf files required embedding the file so it would work. doing this dynamically was not an issue where the issue came up was when they upload other files formats such as jpeg or gif as they require different image related display properties.
so we had to use a show if based on the file extention in other words if the file was a jpeg or gif show it but if it was a swf file then only show that.
what we ended up doing was creating a variable like this:
the file name was header.swf
to pull out the swf we did this
<?php $file=substr($row_rssettings['header'],-3); ?>
Explanation : substr — Return part of a string [ string substr ( string $string , int $start [, int $length ] ) ]
Example Using a negative start <?php $rest = substr("abcdef", -1); // returns "f" $rest = substr("abcdef", -2); // returns "ef" $rest = substr("abcdef", -3, 1); // returns "d" ?>
This variable $file grabs the last 3 characters in the file name - in our case swf
in order to use it in a show if function this is how we did it
<?php // Show IF Conditional region if ($file == "swf") { ?> <embed src="<?php echo $row_rssettings['site_home_page_address']; ?>/admin/prod_images/settings/web/<?php echo $row_rssettings['header']; ?>" width="100%" ></embed> <?php // else Conditional region } else { ?> <img src="<?php echo $row_rssettings['site_home_page_address']; ?>/admin/prod_images/settings/web/<?php echo $row_rssettings['header']; ?>" alt="" align="left" /> <?php } // endif Conditional region ?>
this works fine if you're only allows swf and no other type of files that need to be embedded.
|