1

I'm using the following code to scan a folder for images:

<?php
    $dir = 'img/product/subproduct/cat/';
    $scan = scandir($dir);
        for ($i = 0; $i<count($scan); $i++) {
            $path_parts = pathinfo($scan[$i]); // to remove file extension
            if ($scan[$i] != '.' && $scan[$i] != '..') {
                echo '<img src="' . $dir . $scan[$i] . '" alt="' . $path_parts['filename'] . '" width="50" height="50" />';
            }
        };
?>

And then I display a bigger version of the clicked image and add the 'alt' attribute as a caption:

$('#id img').click(function () {
    var imageName = $(this).attr('alt');
    var chopped = imageName.split('.');
    $('#titlel').empty();
    $('#titlel')
        .prepend(chopped[0]);
    $img = $(this);
    $('#idBig img').attr('src', $img.attr('src'));
});

This works on both localhost and my own server, but as soon as I move it to my client's server the caption does not appear when I click the images.

It's worth noticing that I had to add a .htaccess file with the line "AddHandler application/x-httpd-php5 .php" to my client's server in order for the scandir function to work. Could that be related? how can I fix this?

I appreciate any suggestion.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
brunn
  • 1,323
  • 2
  • 17
  • 37

2 Answers2

2

As documented in the manual, the filename key of the returned array (or PATHINFO_FILENAME) requires php 5.2 or newer. To find out whether that's the problem, check what is outputted by examining the source code of the rendered HTML document.

Also, you should not need to modify an htaccess file for php scripts to work. Instead, modify the global server configuration.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • I forgot to mention, the HTML renders the alt attribute blank but no error appears, as if I had left it blank in the original file. – brunn Nov 07 '11 at 20:48
  • @brunn What's the value of `error_reporting` and `display_errors`? What php version is running on the server that exhibits the problem? – phihag Nov 07 '11 at 20:49
  • PHP version is 5.1.6 and after error_reporting I get: "Undefined index: filename". – brunn Nov 07 '11 at 21:14
1

As phihag mentioned it looks like pathinfo() for 'filename' is available for PHP >= 5.2.0, so if you are running an earlier version you could try (not tested):

$path_parts = pathinfo($scan[$i]);

// Subtract strlen of extension from strlen of entire filename
//  (+1 for '.')
$filenameStrLen = (strlen($path_parts['basename']) - strlen($path_parts['extension'])) + 1;

// Substr basename from 0 to strlen
$filenameWithNoExtension = substr($path_parts['basename'], 0, $filenameStrLen);    

You may want to look into DirectoryIterator as it was built for this type of functionality.

Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
  • Your suggestion worked great, problem solved. I will look into the link you provided to learn some more. Thanks! – brunn Nov 07 '11 at 21:38