0
votes

I have a dir structure like:

IMAGES/
       imgex2.png
       s_imgex2.png
       imgre.png
       s_imgre.png

Where imgex2.png and imgre.png are the big images and s_imgex2.png and s_imgre.png are thumbnails

I want to fill a gallery with absolute path in php like

<li><a href="www.site.com/images/imgex2.png">
    <img src="images/s_imgex2.png" alt="ex2" />
</a></li>
<li><a href="www.site.com/images/imgre.png">
    <img src="images/s_re.png" alt="re" />
</a></li>

How to fill given a path a gallery with the big image first and then the thumbnail (it has a "s_" at the begining of filename) and put in alt="" the name without the "img" string? the issue here is not always the filenames have "img" so trim is not an option

Other problem is getcwd() returns 'file:///D:/Hosting/2543486/html/site/images' not www.mysite.com

I was doing

<?
$dir = './';
$files = glob( $dir . '*.png');
foreach( $files as $file) {
    $path  = getcwd().$file;
    $path  = substr_replace($path, 'file:///D:/Hosting/2543486/html/', 0); 

    $site       = "http://www.site.com/".$path.$file;
    $thumb_site = "http://www.site.com/".'s_'.$path.$file;
    $alt        = substr_replace($file, 'img', 0); 

    echo '<li><a href="'.$thumb_site.'">';
    echo '    <img src="'.$thumb_path.'" alt="'.$alt.'" />';
    echo '</a></li>';


}
?>

but getting:

http://www.site.com/file:///D:/Hosting/2543486/html/site/imgex2.png
http://www.site.com/s_file:///D:/Hosting/2543486/html/site/imgex2.png

How to solve it?

1
Is your site root points to Hosting\2543486\html\site or Hosting\2543486\html? - Passerby
oh sorry I wrote it wrong it prints Hosting\2543486\html\site - edgarmtze

1 Answers

1
votes

I would go for this:

$dir = './';
$files = glob( $dir . 's_*.png');
foreach( $files as $file) {

    $site       = "/images/".str_replace("s_","",$file);
    $thumb_site = "/images/".$file;
    $alt        = str_replace(".png","",str_replace("s_img","",$file)); 

    echo '<li><a href="'.htmlentities($site,ENT_COMPAT,"UTF-8").'">';
    echo '    <img src="'.htmlentities($thumb_site,ENT_COMPAT,"UTF-8").'" alt="'.htmlentities($alt,ENT_COMPAT,"UTF-8").'" />';
    echo '</a></li>';


}

Edit:

If you really really want to use absolute path, simply change it a little bit:

$root="http://www.example.com";
$dir = './';
$files = glob( $dir . 's_*.png');
foreach( $files as $file) {

    $site       = $root."/images/".str_replace("s_","",$file);
    $thumb_site = $root."/images/".$file;
    $alt        = str_replace(".png","",str_replace("s_img","",$file)); 

    echo '<li><a href="'.htmlentities($site,ENT_COMPAT,"UTF-8").'">';
    echo '    <img src="'.htmlentities($thumb_site,ENT_COMPAT,"UTF-8").'" alt="'.htmlentities($alt,ENT_COMPAT,"UTF-8").'" />';
    echo '</a></li>';


}