Jump to content

Image Glob Function


TAV1702

Recommended Posts

Ok gurus, I need your help once again.

 

This time I am up to no good. Well, it is good so yeah. Anyhow, What I want to do is use some code to pull every image in a directory and display it on a page. I am attempting to do this using the glob function. Problem is, it is wanting to put index.php before my image directory and that breaks the images obviously. if I use the old  ../ trick, the page goes blank. If I try to hard code the entire url (which i know is not right and not to do but for testing), same thing, page goes all blank. I have even tried to , even though I knew better, to use use the old php echo url thing and no such love, once again, blank page.

Can you please take a peak at my code and let me know if anything stands out at you?

 

<?php
$files = glob("./lib/signatures/background/*.*");

for ($i=0; $i<count($files); $i++) {
    $image = $files[$i];
    print $image ."<br />";
    echo '<img src="'.$image .'" alt="Profile Badge" />'."<br /><br />";
}

notice i added a single ./ ? That does NOT make the page go blank but instead it displays this:

./lib/signatures/background/background.png
Profile Badge

./lib/signatures/background/background2.png
Profile Badge

 

Now If I am trying to reinvent the wheel, please feel free to call me out on it. I can take it. If there is an easier way to pull off what I want to do, by all means please do crack me up side the head and say listen up Ray. Maybe this little lesson will enrich us all in a new style of doing things or even a page to use on our va website. I think the one thing phpVMS has always lacked was the preview badge feature. It has aggravated me more than once.

Esentially what this is supposed to do is, if a VA has a vast number of sig badges, say 20 for argument sake, well all we can do as pilots is pick a image name and save and hope we picked a good badge. With this code, there would be a link right next to where one chooses the badge while editing profile that would take them to a created page of my choice , badge_options or what ever and on this page, it would pull every image and display it in list fashion, non clickable. And once a pilot see the badge that he or she so chooses to use, they click the back to profile link and bingo bango, they choose their badge and all is well in the neighborhood.

If I right click on where it says Profile Badge, it give me the entire url to my site including index.php in front of the directory. http://XXXXXXXX/XXXXXXXX/index.php/lib/signatures/background/background2.png

Thanks for taking a look at my post. I appreciate it.

Edited by TAV1702
Typos
Link to comment
Share on other sites

I just whipped this up quickly so it might or might not work (bit hacky)

Make a php file called imgcount.php

<?php
$directory = 'lib/signatures/background/';
$ext = '.png';
$files = glob($directory.'*'.$ext);
if($files !== false) { $filecount = count($files); } else { $filecount = 0; }
$num = 1;

while($num <= $filecount && $num > 0) {
    echo '<img src="'.$directory.$num.$ext.'" alt="'.$num.'" />';
    $num++;
}
?>

Being a bit 'hacky' so to speak, you need to have a few things in place for it to work

  1. Permissions set correctly on the images and the folder itself to allow the script to read it
  2. The filename of the images must be set to a number in order (as in 1.png, 2.png, etc.)
  3. The script must be placed in the root directory of the installation
  4. The script can be accessed by going to http://url.com/imgcount.php

Of course, you can modify this all you like as there are many ways to re-code this, and even make it into a module (and utilize some of the API functions if you can find some) and change directories and file extensions, etc. but this was just a quick sketch.

Edited by web541
Link to comment
Share on other sites

Thanks Web, I'll take a stab at this and let you know how ti goes man. I really appreciate the reply.

**EDIT**

Hey Web, that worked. thanks a million man. I really do appreciate your effort on that. One thing I did need to do was echo a couple line breaks as the images were horizontal and not vertical. Now All I have to do is get it in to a module so I can skin the page to match the rest of my site.

Thanks gain. It is genuinely appreciated. I hope others can get some use out of this code snippet as well or maybe a badge preview feature could be worked in to the next release of phpVMS out of this code snippet.

Got to run for now. Take care man.

Ray

Edited by TAV1702
No Bump Edit
Link to comment
Share on other sites

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.

Loading...
×
×
  • Create New...