Jump to content

IF is file print all others


m_tyhurst2002

Recommended Posts

Hello guys and gals, I am pretty green to PHP! I have an empty array that that I am trying to put images into. The thing is I have a certain file name in the folder, I want to exclude that file. This is what I have tried, any advice would be appreciated!

 

$thumbImg[] = array();
foreach (glob($DImg) as $PImg)
{
	if (!is_file("thumbnail.jpg"))
	{
		$thumbImg[] = "<img src=\"pathtoimage\">";
	}
}

 

Later on the page I am printing it out with this. It is still including the thumbnail.jpg image. Thank you in advance!!

 

for ($i=0; $i<count($thumbImg); $i++)
print $thumbImg[$i];

Link to comment
Share on other sites

is_file will check if the file exists on the server, nothing related to the current file in your array. You need to compare it directly:

 

if (basename($PImg) != 'thumbnail.jpg')
{
    // ...
}

 

Though if you plan on expanding this list, you might want to create an array of exceptions and then check the current file in your loop is not within the exceptions array:

 

$exceptions = array(
    'thumbnail.jpg',
    'another-file.jpg'
);

[...]

if (!in_array(basename($PImg), $exceptions))
{
    // ...
}

 

Edit

 

Define your exceptions array before the loop by the way.

Link to comment
Share on other sites

Awesome, thank you very much!! It works beautifully with the $exceptions array. I do have one more question. Where I am printing the FOR loop, it outputs the word "array" right before the images. Is the loop incorrect somehow? Thank you so much for your help!

 

<?
for ($i=0; $i<count($thumbImg); $i++)
	print $thumbImg[$i];
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.