Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: havok4615 - lokakuu 10, 2013, 10:50:23 AP

Otsikko: SMF and $files = glob
Kirjoitti: havok4615 - lokakuu 10, 2013, 10:50:23 AP
Hi, i got a big problem, if i put this PHP code in an custom php block it works, but if i go to the public site it cant find the images/
<?php
$files = glob/Themes/default/images/hexcards/*.*");
for ($i=1; $i<count($files); $i++)
{
$image = $files[$i];
print $image ."<br />"; echo '<img src="'.$image .'" alt="Random image" />'."<br /><br />"; }
?>


The problem is, it puts the index.php in between, instead of using: http://domainname.de/Themes/default/images/hexcards/*.*
it uses http://domainname.de/index.php/Themes/default/images/hexcards/*.* and doesnt find the images.

Ist there a way too fix that?
Otsikko: Re: SMF and $files = glob
Kirjoitti: Arantor - lokakuu 10, 2013, 10:59:23 AP
Yeah, don't use relative paths.

global $settings;
$files = glob($settings['default_theme_dir'] . '/images/hexcards/*.*");


Replace the first line of your code with that and it'll use the absolute path SMF has rather than any relevant path.

I'd also suggest changing your for to a faster version:
for ($i=1, $len = count($files); $i<$len; $i++)

While a count() is not particularly expensive, not recalculating it every single time is a saving, and it is a good habit to get into.
Otsikko: Re: SMF and $files = glob
Kirjoitti: Bob Perry of Web Presence Consulting - lokakuu 10, 2013, 11:18:47 AP
Lainaus käyttäjältä: Arantor - lokakuu 10, 2013, 10:59:23 AP
Yeah, don't use relative paths.

... not recalculating it every single time is a saving, and it is a good habit to get into.

Listen to Arantor you should friend, excellent advice from someone well versed in SMF as well as other platforms...
Otsikko: Re: SMF and $files = glob
Kirjoitti: havok4615 - lokakuu 10, 2013, 12:09:24 IP
    <?php
global $settings;
$files glob($settings['default_theme_dir'] . '/images/hexcards/*.*");

  for ($i=1, $len = count($files); $i<$len; $i++)

  {

  $image = $files[$i];


  echo '

<a href="'.$image .'"><img class="hexcards" src="'.$image .'" alt="Random image" /></a>';
   }

 ?>


This doesnt work at all.
Otsikko: Re: SMF and $files = glob
Kirjoitti: Arantor - lokakuu 10, 2013, 12:10:51 IP
Oh, that's my bad, because I changed over from using the icky " to '

Change the " in your glob to '.
Otsikko: Re: SMF and $files = glob
Kirjoitti: havok4615 - lokakuu 10, 2013, 12:12:45 IP
Okay works, but still not the correct source:

http://hextcg.de/home/www/web78/html/Themes/default/images/hexcards/hextcg269.jpg

This is the link now :(
Otsikko: Re: SMF and $files = glob
Kirjoitti: Arantor - lokakuu 10, 2013, 12:20:07 IP
Huh, I forgot how glob() worked including the full path so it wouldn't ever have worked properly for you anyway (mostly because there are much better alternatives)

<?php
global $settings;
$files scandir($settings['default_theme_dir'] . '/images/hexcards');

foreach (
$files as $thisfile)
{
    if (
$thisfile[0] != '.')
        echo 
'<a href="'$settings['default_theme_url'], '/images/hexcards/'$thisfile'"><img class="hexcards" src="'$settings['default_theme_url'], '/images/hexcards/'$thisfile'" alt="Random image" /></a>';
}

?>
Otsikko: Re: SMF and $files = glob
Kirjoitti: havok4615 - lokakuu 10, 2013, 12:24:16 IP
Works like a charm. Thank you.