Uutiset:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu
Advertisement:

SMF and $files = glob

Aloittaja havok4615, lokakuu 10, 2013, 10:50:23 AP

« edellinen - seuraava »

havok4615

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?

Arantor

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.
Holder of controversial views, all of which my own.


Bob Perry of Web Presence Consulting

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...
Best Regards,
Bob Perry



"The world is moving so fast these days that the man who says it can't be done is generally interrupted by someone doing it." Elbert Hubbard

havok4615

    <?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.

Arantor

Oh, that's my bad, because I changed over from using the icky " to '

Change the " in your glob to '.
Holder of controversial views, all of which my own.


havok4615


Arantor

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>';
}

?>
Holder of controversial views, all of which my own.


havok4615

Works like a charm. Thank you.

Advertisement: