Show small number of images in foldergalery - php

Im having trouble finding some piece of code for showing me the small number of how many images is in a folder in my website. Im using foldergallery.
I need to show the number above my folders, so one would know how many images are in a folder.
Can someone please point me there or write some kind of PHP code for this.
Thank you
R

You can use this:
$directory = "/path/to/directory/";
if(is_dir($directory)) {
$images = glob($directory . "{*.jpg,*.JPG,*.jpeg,*.png,*.PNG,*.gif,*.GIF}");
$numberOfImages = count($images);
}
After that, you just print the $numberOfImages where you want.

Related

Echoing fixed number of images on a single row

How are these images being shown. Are they displaying it by calling the php echo on the set html table or do they just echo it using while loop. The data are taken from the database. and how do they make the 4th image a new row after 3 images are displayed. Beginner here. Thanks.
The Basic Principle is Keep track of the total number of pictures you add with a variable, say $reconi, and if the modulus (Remainder of Division by 3) is 0, then add a line break (In the example below I am showing Images pulled from a Directory, I already had the script handy, but you shall easily adapt it for doing from database as well). If not clear, feel free to get back.
$directory = "D:/ItemPics/";
$files = glob($directory . "*");
$reconi=1;
foreach($files as $file){
if($reconi % 3==0){
// Add picture
// Add a line break
}else{
// Add picture;
}
$reconi++
}

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

Delete specific filenames in an array using PHP

I have a list of PDFs and I would like to delete them. Can someone please explain to a beginner how this is done? At this point, I can do this in Excel a number of different ways but how can I simply do this in PHP?
Most simple solution:
<?php
$dir = '/files_directory/';
$files_to_delete = array('file1.pdf', 'file3.pdf', 'file4.pdf');
foreach($files_to_delete as $file)
{
$file_path = $dir . $file;
if(is_file($file_path))
{
unlink($file_path);
}
}
Use PHP to search for the folder, do a loop through the files there and add some conditional statements to the loop. If they are true or false, act on those results.
As you have not typed any code for us, I am not going to type any code back to you.
The most simple way I could think of doing it:
array_map('unlink', $array_with_files_to_delete);
$array_with_files_to_keep = array_diff($list_with_pdfs, $array_with_files_to_delete);

PHP glob() breaks on WordPress sub pages

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/*.*')

check if file exists in directory with .css extension based on social engine playlist title

I'm struggling to wrap my head around some code im working on with a social engine based site. the basics of what I want to accomplish is to check if a file exists in a folder called /customcss/ for each playlist title with a .css extension. $playlist->getTitle is used to get the playlist title.
Can someone point me in the right direction? I've been struggling with this for a few hours now, and since SE isnt open source, there is little info on the web.
Here is my code so far, which is not yeilding any error, but isnt working either...
<?php
$filename = '/customcss/$playlist->getTitle.css';
if (file_exists($filename)) {
echo 'official';
} else {
echo 'not official';
}
?>
Best,
$filename = '/customcss/'.$playlist->getTitle.'.css';
OR
$filename = "/customcss/{$playlist->getTitle}.css";
$filename = '/customcss/$playlist->getTitle.css';
would pick from root
please use an absolute path
like
$filename = _DIR_ . '/customcss/$playlist->getTitle.css';
Vicky Biswas

Categories