On my website's dashboard, I use a while loop to retrieve data from a DB and echo each record. Inside this while loop, I use glob() to show all the images inside the specific folder, which is a column in my DB table, $record["folder"].
This is the code I use:
while($record = mysqli_fetch_array($result))
{
$directory = '../send-us-your-photos/media/'.$record['folder'];
chdir($directory);
$images = glob("*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
foreach($images as $image) {
echo '<div class="col-sm-4">
<img src="'.$directory.'/'.$image.'" />
<div>'.$image.'</div>
</div>';
}
}
Now, the first record works fine and is showing the images in the specific folder. But the next records all show the same $image data but for a different folder.
So for example:
Record 1: folder 1 - shows image1.jpg
Record 2: folder 2 - tries to find same image1.jpg in folder 2
Record 3: folder 3 - tries to find same image1.jpg in folder 3
What I need:
Record 1: folder 1 - shows images in folder 1
Record 2: folder 2 - shows images in folder 2
Record 3: folder 3 - shows images in folder 3
How should I change my code so it shows the correct images in each record?
simply , don't change your directory , and use your directory path directly in glob function
while($record = mysqli_fetch_array($result))
{
$directory = '../send-us-your-photos/media/'.$record['folder'];
// chdir($directory);
$images = glob($directory . "/*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
foreach($images as $image) {
echo '<div class="col-sm-4">
<img src="'.$directory.'/'.$image.'" />
<div>'.$image.'</div>
</div>';
}
}
I think it's down to your chdir statement. You are going down one folder .. then up to send-us-your-photos/media/_FOLDER_.
So the next time you start off in send-us-your-photos/media/_FOLDER_ and go back one folder .. to send-us-your-photos/media/ and then it tries to go to send-us-your-photos/media/send-us-your-photos/media/_FOLDER_ and obviously fails.
It doesn't look like it's failing because you're outputting the value of $directory which you set yourself. Try outputting getcwd() instead.
Related
How can i display all images from an ftp server in website using php?
It should also sort automatically so i could just add more images to the ftp server and it would work.
I'm trying to make a gallery type thing
I've already looked through google and found nothing that worked.
I'm still learning php and dont know too much about it.
Any help would be much appreciated. Thanks!
As already commented, scandir does the trick. The below code gets all the images and output them as <img> elements.
index.php:
// Here enter the base url of the images folder
$base_images_url = "http://example.com/images/";
$dir = dirname (__FILE__) . "/images/";
// Get the files in the folder. Sort in ascending order - this is the default
$images = scandir($dir);
// Or is you need to sort in descending order
//$images = scandir($dir,1);
// Output an img tag for every image.
foreach($images as $image){
// skip the . and .. that starts the folder listing
if($image === '.' || $image === '..'){
continue;
}
$image_url = $base_images_url . $image;
echo '<img src="' . $image_url . '">';
}
This works with the following folder setup:
-index.php
-images
-image1.png
-anotherimage.jpg
I have used a function
/*
Random File Function
Written By: Qassim Hassan
Website: wp-time.com
Twitter: #QQQHZ
*/
function Qassim_Random_File($folder_path = null){
if( !empty($folder_path) ){ // if the folder path is not empty
$files_array = scandir($folder_path);
$count = count($files_array);
if( $count > 2 ){ // if has files in the folder
$minus = $count - 1;
$random = rand(2, $minus);
$random_file = $files_array[$random]; // random file, result will be for example: image.png
$file_link = $folder_path . "/" . $random_file; // file link, result will be for example: your-folder-path/image.png
return '<img src="'.$file_link.'" alt="'.$random_file.'">';
}
else{
return "The folder is empty!";
}
}
else{
return "Please enter folder path!";
}
}
?>
to pull a random image from a specific wordpress directory. I can successfully call up a random image file however it does not display, just displays the image filename and a broken image icon.
The code I am using from the tutorial to display the image is <?php echo Qassim_Random_File("my-folder-name"); // display random image! ?>
I am brand new to php as of this week, and a fairly novice coder in general (know some basics) so I really am stumped. I've fooled with other possible solutions from stack overflow already but this tutorial is the only thing I've been able to get close to working. Many thanks if anyone can spot the problem!
This is showing up in the HTML inspector:
<div class="elementor-widget-container">
<h5>call</h5>
<a href="wp-content/uploads/H-PH-MA-Greyscale/image_2.jpg" target="_blank" title="image_2.jpg">
<img src="wp-content/uploads/H-PH-MA-Greyscale/image_2.jpg" alt="image_2.jpg">
</a>
</div>
You just need to replace this line:
$file_link = $folder_path . "/" . $random_file;
...with this one:
$file_link = get_site_url(null, $folder_path . "/" . $random_file);
What was the problem?
scandir is returning the physical file path, not the url to the file - we can confirm this is we look at one of the values it returns for $random_file, e.g. wp-content/uploads/H-PH-MA-Greyscale
This is a relative URL, but we need an absolute URL so that it will work no matter where it is called. We also need to include the site URL so that the path to wp-content is correct. (Note: site URL and home URL can be different - the site URL is the one that always point to the location of the WP files).
Another problem is the trailing slash (or lack of one) on the site URL. get_site_url and get_home_url do not add the trailing slash to the URL, so you need to add it yourself if it isn't included in the file path.
So how do we solve this?
Using the get_site_url function added the correct path to the WP files, and it also lets us pass it the file path with our without a preceding slash as a parameter, and it will add the slash on the path only if it is needed.
The code looks simple, if the folder exist and it has more then 2 file (it considers the "." and ".." and files when you use scandir) then you should be able to see something. What's the generated HTML code for that function call?
Be aware that the code you are using builds the file url using the phisical path, that isn't going to work. You should fix this part:
if( $count > 2 ){ // if has files in the folder
$minus = $count - 1;
$random = rand(2, $minus);
$random_file = $files_array[$random]; // random file, result will be for example: image.png
$file_link = get_home_url().$folder_path . "/" . $random_file; // file link, result will be for example: your-folder-path/image.png
return '<img src="'.$file_link.'" alt="'.$random_file.'">';
}
Notice the get_home_url() to build the real URL which is needed for your <img tag src
in php i want to ask how to get images one by one from folder directory and display in the jquery slider i have tried this php code by it's not working as i want?
<ul class="jcarousel-list">
<li class="jcarousel-item">
<?php
$directory = "data/uploads/topslider/";
if (glob($directory . "*") != false)
{
$filecount = count(glob($directory . "*"));
}
else
{
}
$files_index = glob("data/uploads/"."top"."slider/*.*");
for ($i=0; $i<$filecount; $i++)
{
$num2 = $files_index[$i];
?>
<img src="<?php echo $num2;?>" width="50" height="50" alt="" /> <?
}?></li>
</ul>
i want display like this:
Image1 Imag2 Image3......and So On from single folder or directory
There are four main things to check:
Is your $directory path correct relative to your current working directory in PHP? You can determine your current working directory by doing a temporary echo getcwd();. It needs to return the parent directory of your "data" folder for your code to work.
Is the data folder accessible from the current page on the web? e.g. if you manually remove the page from the URL (say, index.php) and add data/uploads/topslider/someimage.png where someimage.png is an image you know exists in your topslider folder, does the image load properly in the browser? If not, you'll need to update the way you build the src attribute in your img tag(s).
You're only adding one jcarousel-item for all your images, which doesn't seem right to me. I'm guessing you're expected to add a list item for each image.
You don't need to call glob twice just to ascertain how many files you have to work with. Just do a foreach statement once:
echo '<ul class="jcarousel-list">';
foreach(glob($directory . '*') as $filename) {
echo '<li class="jcarousel-item"><img src="'.$filename.'" width="50" height="50" alt="" /></li>';
}
echo '</ul>';
I am trying to make a simple gallery script on my page for a photo booth. I have a tethered camera which is dropping pictures into a folder on my local site hosted with MAMP. Each folder is a different groups photos. Path to photos is ('/images/**'). First page searches the folders in my images directory and returns only the first image as a thumbnail/link to all folders containing all images for that group of people.
My structure is as follows.
1st page is:
$i = 1;
foreach (glob('images/*') as $dir) {
echo '<div id="strip' . $i . '" class="polaroid">';
// One div per directory
$p = 1;
foreach (glob($dir."/*.jpg") as $img) {
$p++;
if ($p <= 2) {
echo "<a href='$dir'><img src='$img'/></a>";
} else {
}}
echo "</div>\n";
$i++;
This part works and takes the user to the directory with jpegs in it. which also contains a index.php file with the following code:
$dir = 'images/'.basename('/'.getcwd()).'/';
foreach (glob($dir."/*.jpg") as $img)
echo $img;
{
echo "<img src='$img'/>";
}
This is where it fails. When I echo out $dir it shows the current relative path, but $dir in the foreach statement seems to be returning empty. This just returns an empty img tag. Any opinions where I am going wrong.
Thanks in advance
If you want to list the files in the current directory you have to set
$dir = '.';
You did set $dir to /images/225652/ but you are already in this directory
Currently I have a script running to grab all the images in a directory based on ending with "154x154.jpg". Basically what I'm doing is going through a gallery directory and looking for all the thumbnails of images, which I will then output in a sliding gallery on the main page of a website.
<?php
// loop through the images
$count = 0;
$images = array();
foreach (glob("../../uploads/2012/05/*154x154.jpg") as $filename) {
$images[$count] = $filename;
$count++;
}
for ($i = 0; $i < 7; $i++) {
$random = mt_rand(1, $count - 1);
echo '<li><a class="gal_img" href="#">';
echo '<span class="roll"></span>';
echo '<img class="image" src="'.$images[$random].'" height="154" width="154" alt="" />';
echo '</a></li>';
}
?>
This works perfectly fine as it is, but what I need to do is be able to grab the files from within a directory above the current one. To be more specific:
image folder: http://root/wp-content/uploads/2012/05/
theme folder: http://root/wp-content/themes/theme_folder/
** EDIT **
Everything works now with the above code it executes fine, when I go directly to the file. However using it within the theme generates everything except for the filepaths in the code. Using wordpress 3.3.2
Try:
foreach (glob("../images/*154x154.jpg") as $filename) {
Update:
Of course, this is a relative path and only works if the file you need to include is one level up from your script's current working directory. Solution: use an absolute path, ie. one that begins with /. Also, keep in mind that the absolute path is a path on the disk, not the root folder of your website.