Problem using glob() to get thumbnails

Hi guys, I am trying to get thumbnails out of a directory and display them, but for some reason it is not working and I am lost as to why as the variable I am using works just fine earlier in the code. I am about to have a mental breakdown over this. :frowning:

My image directory is set up like this:

/items/company/item_name/*images go here ie: A_full, A_thumb, B_full, B_thumb.

First I am getting the items information for display (note, I am sanitizing everything, just shortening this example):

if (isset($_GET['itemurl'])) {
$item-url = $_GET['itemurl'];
$get_item = mysqli_query($link, "SELECT items.*, company.company-name FROM items INNER JOIN company ON items.company-id=company.id WHERE items.item_url = '$item-url'");
	$row = mysqli_fetch_assoc($get_item);
		if (!$row) {
			echo 'Error.';
			exit();
		}
$image_directory = "/items/$item-url/"; // for this example, $item-url = company-name/item-name-one/
// I also set a bunch of other variables for item display here

Then I display the item:

$item-thumb = "$image_directory" . "top.jpg";
echo "<div><img src='$itemthumb'></div>";

// bunch more item stats here etc, then lastly:

echo '<div>';
    $images = glob($image_directory."*_thumb*");
	if (!$images) {
		echo 'No images found.';
	}
	else {
	foreach ($images as $image) {
            $full = str_replace('thumb','full',$image);
	    echo '<a href="'.$full.'" target="_blank"><img src="'.$image.'"></a>';
	}
 }
echo '</div>';

Now what doesn’t make sense to me is…the thumbnail I have linked to using the $image_directory variable at the top of the ‘display the item’ portion of my code displays correctly…it’s only the thumbs at the bottom that aren’t showing up (I get the ‘No images found’ error I have set, yet the variable is the same…and all of those images are in the same directory…

Any help on this would be appreciated more than I can say as I’m losing my mind a bit, not to mention sleep.

Shouldn’t _thumb have a trailing / slash?

I would view the source and see what images exactly the script is searching for.

Well top.jpg earlier in my script is in the same directory as the rest of the items images (A_full.jpg, A_thumb.jpg, B_full.jpg, B_thumb.jpg). It uses the same variable - $image_directory - to reach it, and it works perfectly. This is alone enough to make me really confused as to why it wouldn’t work 20 lines down in the same piece of code.

I added an echo “$image_directory”; right before the glob() line, and it is outputting the correct directory the images are stored in, too.

The item page is reached via mod_rewrite, but I don’t think that matters as the top thumb works fine…is something wrong with my glob() code?

Yeah I disabled my rewrite rule and went to the item directly, the top image displayed but the bottom images still did not. I am slowly drifting into painful madness.

What is _thumb? The first time I read it I thought it was a directory for some reason.
Do all the thumbnail iamges have _thumb appended to their names?

So I’ve done some playing.

I reverted back to my working code.

To access the product I go to

http://www.mysite.com/products/index.php?id=20

This brings up the information for the product with id 20. Works fine, the thumbs pull and display fine etc. Nothing broken whatsoever.

But apparently it IS when I implement any sort of rewrite rule that the thumbs break. So something is going on there that hopefully someone can help me figure out.

Here is an example rewrite rule I am trying. I kept it simple purely for testing:

RewriteRule ^products/(.*)/(.*)/$ /products/index.php?id=$1&model=$2 [L]

Then in /products/index.php my product links look like this:

<a href="/products/$product-id/$product-model-name/">Product Name</a>

This works and is displaying the products information…via the script below (remember I am sanitizing etc), this is just for testing:


if (isset($_GET['id']) && isset($_GET['model'])) {
$id = $_GET['id'];
$m = $_GET['model'];
$get_product_stats = mysqli_query($link, "SELECT products.*, company.company_name FROM products INNER JOIN company ON products.company_id=company.id WHERE products.id='$id' AND products.model='$m'");
	$row = mysqli_fetch_assoc($get_product_stats);
		if (!$row) {
			echo 'Error';
			exit();
		}
	$company_name = $row['company_name'];
	$product_model = $row['model'];
	$product_model_abbr = $row['product_model_abbr'];
	if ($row['product_model_version'] == NULL) { $image_directory_name = "/products/$company_name/$product_model_abbr/"; } else $image_directory_name = "/products/$company_name/$product_model_abbr (" . $row['product_model_version'] . ")/";

$top = "$image_directory_name" . "top.jpg";
echo "<div><img src='$top'></div>"; // THIS DISPLAYS FINE.

// echo some more stats here

Now here is the problematic area…

echo '<div>';
	$images = glob($image_directory_name."*_thumb*");
		if (!$images) {
			echo '<p>Currently no images for this product.</p>'; // I get this error
		}
	else {
		foreach ($images as $image) {
			$full = str_replace('thumb','full',$image);
			echo '<a href="'.$full.'" target="_blank"><img src="'.$image.'"></a>';
		}
	}
echo '</div>';

So, again, the top thumbnail image shows, but the bottom ones still do not. But this only occurs after implementing the RewriteRule. So that tells me it’s gotta be the RewriteRule…right?

:confused:

Sorry, been typing a long reply. Yeah that’s correct, all images, full-size and thumb are in the same directory (/products/company-name/product-name/). Full size images are appended by _full, thumbs are appended by _thumb.

Ok I think I’m very close to solving this!! IF someone could please chime in that would be great…I am so close!

I have stripped my site down to the bare minimum to troubleshoot this, and I have found the line that is causing me trouble, but I just don’t know WHY it is causing me trouble. Here is my code as it stands:

//display all products
if (!isset($_GET['id'])) {
	$result = mysqli_query($link, "SELECT products.*, company.company-name FROM products INNER JOIN company ON products.company-id=company.id");
		if (!$result) {
			echo 'Error';
			exit();
		}
}
//display one product
if (isset($_GET['id'])) {
$id = $_GET['id'];
$get_item_info = mysqli_query($link, "SELECT products.*, company.company-name FROM products INNER JOIN company ON products.company-id=company.id WHERE products.id='$id'");
	$row = mysqli_fetch_assoc($get_item_info);
		if (!$row) {
			echo 'Error';
			exit();
		}
	$company_name = $row['company-name'];
        $model_abbreviation = $row['model-abbreviation'];
	if ($row['model-version'] == NULL) { $image_directory_name = "$company_name/$model_abbreviation/"; } else $image_directory_name = "$company_name/$model_abbreviation (" . $row['model-version'] . ")/";
}

if (!isset($_GET['id'])) {
	$group = array();
		while ($row = mysqli_fetch_assoc($result)) {
		$group[ $row['company-name'] ][] = $row;
		}
		foreach ($group as $company_name => $models) {
				foreach ($models as $model) {
						echo "<a href='$model[id]/'>$model[model-abbreviation]</a>";
				}
		}
}
if (isset($_GET['id'])) {
	$top_thumb = "$image_directory_name" . "top.jpg";
	echo "<img src='$top_thumb'>"; echo '</br>';
			$images = glob($image_directory_name."*_thumb*");
			if (!$images) {
				echo 'No images to display.';
			}
			else {
				foreach ($images as $image) {
				$full = str_replace('thumb','full',$image);
					echo '<a href="'.$full.'" target="_blank"><img src="'.$image.'"></a>';
				}
			}
}

So if I use this code in combination with my rewriterule:

RewriteRule ^products/(.*)/$ /products/index.php?id=$1

And I go to a product page, such as localhost/products/30/

No images display, HOWEVER, in the page source I can see that it is TRYING to display the images (though I get no error from my script, just a blank page), just with the wrong path - it looks like this:

<img src='Company Name/Model Name (Model Version)/top.jpg'></br><a href="Company Name/Model Name (Model Version)/A_full.jpg"><img src="Company Name/Model Name (Model Version)/A_thumb.jpg"></a>

So the code is working, but the path is wrong. OK.

so I add /products/ to this line:

if ($row['model-version'] == NULL) { $image_directory_name = "/products/$company_name/$model_abbreviation/"; } else $image_directory_name = "/products/$company_name/$model_abbreviation (" . $row['model-version'] . ")/";

This is where it breaks.

The top thumb is fixed and displays, but I get my error when trying to find the bottom thumbs - ‘No images to display’. And the source doesn’t even show as trying to load them anymore.

So. I have gotten really close, I’ve spent countless hours on this. Anyone got any idea what’s going on? Something to do with my path I’m guessing…

When trying to find out what’s causing problems with a script, on a development server you should set PHP’s error reporting to report absolutely everything and to display errors (the display of errors should always be disabled for a live server).

Do a var_dump on $image_directory_name to check that it’s what you expect it to be.

On a more important note, your code is wide open to potential SQL Injection attack as you’re letting user submitted data near the database without having first at the very minimum escaped it. You should really be making use of prepared statements.

Sorry I should have said, I am escaping everything, I just trimmed everything down purely to narrow down this problem, once I do it’ll all be secured properly again…just stripped away every piece of code I could until I found what is (potentially) causing the issue.

Will try var_dump.

Thank you for replying! I’ve been sitting here all day and all day yesterday going nuts over this.

edit: just did var_dump, and it is giving me what I’d expect.

string(23) “/products/companyname/productname/”

That is where the images are (it’s where top.jpg is too, and that is displaying JUST above where I’m trying to display these thumbs)

To give as much information as possible, when I go to localhost/products/companyname/productname/ in my browser, I get Error from the if (!$row) line in my code. However if I go to localhost/products/companyname/productname/imagename.jpg, the image loads. Not sure if that matters, just putting everything out there.

Bit more info, I tried moving all of my images to a different physical folder.

From /products/companyname/productname/

to /images/companyname/productname/

The bottom thumbs still wouldn’t show up, yet the top.jpg thumb still did.

This is the last thing I have to do on my site, so right now I feel completely defeated, and if I can’t figure this out, I can’t have good urls for my product pages, they will be ugly index.php?id=2024 urls :frowning:

Does anyone have any idea how I can test further? I really don’t know what else to do even for testing…this just makes no sense

If it’s a development server that you’re testing on, turn on the display of errors and set the error reporting level to maximum. Have you checked the file permissions?

Error reporting is already set to E_ALL (the only error I get is from my own code on the glob() - ‘No images to display’), and the image permissions are alright as everything loads/works fine until I implement the RewriteRule.

It’s interesting.

if I remove /products/ from the $image_directory_name variable (where it is set)

and I change my glob() code to this:

$folder = "$image_directory_name";
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
for ($i = 0; $i < $count; $i++) {
    echo '<a name="'.$i.'" href="#'.$i.'"><img src="'.$files[$i].'" /></a>';
    echo substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder));
}

It displays a complete text list of images in the directory, but no actual images. And if I view the source, I get this for the images:

<img src=“company/product/file1.jpg”/>

and if I click on that link in the source, it attempts to find the image at:

/products/ID/company/product/file1.jpg

@oknow; my gut feeling at this point is that it’s the Mod Rewrite that’s breaking it. I’m going to move this thread over to the Server Configuration, Apache & URL Rewriting. Could you post the whole of the relevant htaccess file?

Thanks for that it is appreciated. I agree it has to be the Rewrite.

Here is my entire .htaccess file:

RewriteEngine On
RewriteRule ^products/(.*)/$ /products/index.php?id=$1

Removing the trailing slash from the rule:

RewriteRule ^products/(.*)$ /products/index.php?id=$1

Fixed my issue.