PHP glob() breaks on WordPress sub pages - php

I've been struggling with this for a while now.
I've got an image gallery running using jQuery cycle plugin and the files are pulled from a folder using PHP glob(). Problem is, when I navigate to another page the gallery breaks due to the url of the new page being tacked on at the beginning of the file path.
Example:
Front Page url: http://localhost/project/image-display-images/image.jpg
Other Page url: http://localhost/**NEWPAGE**/project/image-display-images/image.jpg
Here's my code:
$files = glob('image-display-images/*.*');
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img src="'.$num.'"'.' alt="Campus Images" width="362" height="246"/>';
}
This would generate a list of images for jQuery cycle to scroll through. It only works on the front page though.
Any ideas?
SOLVED!
Here is my new code:
$files = glob(ABSPATH.'/image-display-images/*.*');
foreach ($files as $f) {
echo '<image src="'.home_url(str_replace(ABSPATH,'',$f)).'"alt="Campus Images" width="362" height="246"/>';
}
This works on all pages.
Thank you!

Use an absolute path:
$files = glob(ABSPATH.'image-display-images/*.*');
The WordPress Core sets the ABSPATH constant so it should be fairly reliable.
glob deals with filesystem paths, but you are trying to load URLs. To display the files the way you are trying to, you will need to convert the results to URLs. Here is a bare-bones example.
$files = glob(ABSPATH."*.*");
foreach ($files as $f) {
echo home_url(str_replace(ABSPATH,'',$f));
}
You may better off writing you own function to grab your file names, rather than depending on glob which does come with a warning about not being available on some systems. See: http://codex.wordpress.org/Filesystem_API

Define the full path of your gallery instead of 'image-display-images/*.*'
For example glob('/var/etc/www/image-display-images/*.*')

Related

Sorting image files using server modifed date. Not File EXIF modified

I have been told to rewrite my question by Stackoverflow
I upload images mostly to a directory on my sever where my website is hosted. Via a program on my PC and a app on phone and that works a treat.
I have come across PHP scripts that shows all the images in the above folder and returns a basic gallery, and they all work to a point.
What I am hoping to achieve (it might not be possible) is list the images by the date they were added to the server. Not sorting from the EXIF modified date.
If it cant be done, so be it.
Best regards,
RT
PS At the moment I'm use PHP Gallery, its OK but does not quite achive the sorting requirement I would like.
Gallery here. https://www.sidingstudios.com/pix4web/index.php
Is it possible to display the contents of server folder (images) but sort by Last Modified ? using PHP
My very basic script:
<?php
$images = glob('img/*');
foreach ($images as $image) {
echo '<img src="'.$image.'"><br>';
}
?>
Link its used on. https://www.sidingstudios.com/pix4web2/
This will sort the images by last modified time
$files = glob('img/*');
$output = [];
foreach ($files as $f)
{
$output[filemtime($f)] = $f;
}
ksort($output);
$images = array_reverse($output);
foreach ($images as $image)
{
echo '<img src="' . $image . '"><br />';
}

How to retrieve all text files from multiple different directories?

You may have heard of the glob method but that only manages to retrieve files from the directory that the file containing the method is located on - only one directory.
This is an example of the code that I am using:
<?php
foreach (glob("*.txt") as $filename)
{
$time = filemtime($filename);
$files[$time] = $filename;
}
krsort($files);
foreach ($files as $file) {
echo $file;
}
?>
What happens here is that all of the text files in the current directory are retrieved, they are then sorted by order of date modified and are then echoed out onto the page.
The problem with this is that I don't just want to retrieve text files from the one directory.
How would I change this so that I can retrieve files from multiple directories of my choice all from one page - so I can echo out all of the text files from the multiple directories onto one page rather than only echoing out the text files from one directory?
I believe I would need to store all the directories I want to glob into an array but I am not sure how to retrieve it.
Here is an example stolen from the page in my comment above
<?php
$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.txt$/i', RecursiveRegexIterator::GET_MATCH);
?>

Pull Images & Thumbnails from a Folder Through PHP

I'm trying to generate images on my website by pulling a thumbnail from one folder and the actual image from another. I have no idea what I am doing. This is what I have so far:
<?php
$thumbdirname = "images/thumbs/";
$imgdirname = "images/";
$mainimages = glob($imgdirname."*.{jpg,png,gif}", GLOB_BRACE);
$imagethumbs = glob($thumbdirname."*.{jpg,png,gif}", GLOB_BRACE);
foreach($imagethumbs as $image) {
echo '<a class="imageLink" href="$mainimages" data-lightbox="logo" data-title="Vivid Logo"><img src="'.$image.'"</a> ';
}
?>
I know that it will not work with just "$mainimages" for href, but I haven't been able to figure out what to put there. As is, it will pull up the right thumbnail, but not link to the full associated image.
I tried to put another foreach statement in, but I get four results from my two pictures (and their thumbnails). Which makes sense sense it is showing one of each combination:
(img1,thumb1),(img1,thumb2),(img2,thumb1),(img2,thumb2)
How should I change this to get it playing nicely?
You don't seem to be outputting from $mainimages so I'm going to ignore this for now.
You PHP code seems to be OK from initial glance. Only thing that is a bit iffy is that you are not closing you image tag.
If that does not help try counting the image array/object.
If that returns 0 try the below code.
// Find all files in that folder
$files = glob('images/gallery1/*');
// Display images
foreach($files as $file) {
echo '<img src="' . $file . '" />';
}
Hope that helps

Get Images In Directory and Subdirectory With Glob

I have a PHP script written to grab all images in a certain directory and display them, although I want to have the ability to display images also in sub directories of the directory I specified.
So for example right now my script is only getting the following images
uploads/prevImgs/145323.png
uploads/prevImgs/276531.png
Where id want it to get the following
uploads/prevImgs/145323.png
uploads/prevImgs/276531.png
uploads/prevImgs/dir1/12323.png
uploads/prevImgs/dir2/212331.png
My current script is the following
<?php
// Directory Path Of Library Preview Images //
$dirname = "uploads/prevImgs/";
$images = glob("{$dirname}*.*");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
?>
Huge thanks in advance!!!
You can stack a RecursiveDirectoryIterator and an RecursiveIteratorIterator to get the effect you're looking for:
$rdi = new RecursiveDirectoryIterator("uploads/prevImgs/");
$it = new RecursiveIteratorIterator($rdi);
foreach($it as $oneThing)
if (is_file($oneThing))
echo '<img src="'.$oneThing.'" /><br />';
I know, it seems counter-intuitive that you have to take RecursiveDirectoryIterator and stack another iterator on top of it to make it work. The documentation in this area could be better.
I can suggest to use a RecursiveDirectoryIterator for creating a class to iterate through Files of (sub)directories and its files. Together with one of the recursive filter iterators php offers you can only return only image files.
There is a GlobIterator too but I'm not sure if it also do recursive iterations.

Select random file from directory

I'm trying to make a site where users can submit photos, and then randomly view others photos one by one on another page. I have a directory called "uploads" where the pictures are submitted. I'm having trouble reading the pictures from the file. I just want to randomly select a picture from the directory uploads and have it displayed on the page. Any suggestions appreciated.
You can use glob to get all files in a directory, and then take a random element from that array. A function like this would do it for you:
function random_pic($dir = 'uploads')
{
$files = glob($dir . '/*.*');
$file = array_rand($files);
return $files[$file];
}
I've turned it a little to get more than one random file from a directory using array.
<?php
function random_pic($dir)
{
$files = glob($dir . '/*.jpg');
$rand_keys = array_rand($files, 3);
return array($files[$rand_keys[0]], $files[$rand_keys[1]], $files[$rand_keys[2]]);
}
// Calling function
list($file_1,$file_2,$file_3)= random_pic("images");
?>
You can also use loop to get values.
This single line of code displays one random image from the target directory.
<img src="/images/image_<?php $random = rand(1,127); echo $random; ?>.png" />
Target directory: /images/
Image prefix: image_
Number of images in directory: 127
https://perishablepress.com/drop-dead-easy-random-images-via-php/
Drawbacks
images must be named sequentially (eg image_1.png, image_2.png, image_3.png, etc).
you need to know how many images are in the directory in advance.
Alternatives
Perhaps there's a simple way to make this work with arbitrary image-names and file-count, so you don't have to rename or count your files.
Untested ideas:
<img src=<?php $dir='/images/'; echo $dir . array_rand(glob($dir . '*.jpg')); ?> />
shuffle()
scanDir() with rand(1,scanDir.length)
Or you can use opendir() instead of glob() because it's faster

Categories