Having Issues on Using PHP glob() in WordPress - php

Using pure PHP code inside WordPress I am having trouble on getting the glob() work to generate image source.
<div class="carousel-inner" role="listbox" style="height=600px; width=1000px;">
<?php
$directory = "http://geocaa.com/wp-content/themes/Booting/img/services/";
$images = glob($directory . "*.png");
foreach($images as $image)
{
echo '<div class="dynamic item">';
echo ' <img src="'.$image.'" alt="...">';
echo ' </div>';
}
?>
</div>
As you can see I tried to hardcoded the $directory as "http://geocaa.com/wp-content/themes/Booting/img/services/"; and also I already investigate on these two Post [Post 1 & Post 2 ] regarding the same issues but the solutions there still not working for me!
The get_theme_root() retuns nothing but the get_template_directory() is returning something which is more like
$images = glob(get_template_directory().$directory . "*.png");
/home/vcbb/public_html/wp-content/themes/geocaa/img/services/img.png
and useless for image src

Try this:
$directory = "/img/services/";
$images = glob(get_template_directory().$directory . "*.png");
foreach($images as $image)
{
echo '<div class="dynamic item">';
echo ' <img src="'. str_replace(get_home_path(), get_home_url(), $image) .'" alt="...">';
echo ' </div>';
}

Related

Wordpress: Using glob() returns an empty array

I am trying to pull all images from a specified directory and then display them. I used the below piece of code in a regular website and it works
<?php $dirname = "images/tile/tile2/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<li><img src="'.$image.'" /><br /></li>';
}?>
I have now moved this code and modified it to a wordpress site and I get an empty array.
<?php $dirname = get_template_directory_uri()."/images/tile/tile1/";
$images = glob($dirname. "*.jpg");
//$images = glob($dirname."*. {jpg}", GLOB_BRACE); - tried with GLOB_RACE
foreach($images as $image) {
echo '<li><img src="'.$image.'" /><br /></li>';
}?>
Second code
<?php define('ACCREDPATH', get_template_directory_uri() . '/images/tile/tile1/');
$images = glob(ACCREDPATH. "*.jpg");
//$images = glob(ACCREDPATH. "*. {jpg}", GLOB_BRACE); - tried with GLOB_RACE
foreach($images as $image) {
echo '<li><img src="'.$image.'" /><br /></li>';
}?>
I have checked that the images are in the folder
I have done a var_dump and I am getting the right path.
var_dump on the glob($images) gives array(0){ }
I have also looked for other threads with this issue and still nothing
Please any help would be useful.
glob works on a local path rather than a URL. The function needs to be able to access the server's filesystem which it can't do remotely (via URL).
http://php.net/manual/en/function.glob.php
The path you're providing in your working code is relative. It functions because it can be used as both a file path and a URL.
To achieve the same result with an absolute path to your theme you'll need to use get_template_directory(). In the example provided I'm using get_template_directory() to get the absolute path and get_template_directory_uri() to output as a URL.
Example:
$theme_img_path = '/images/tile/tile1/';
$images = glob( get_template_directory() . $theme_img_path . '*.jpg' );
foreach ( $images as $image ) {
// Convert the filename into an absolute URL.
echo get_template_directory_uri() . $theme_img_path . basename( $image );
}
There's another issue in your second attempt that's worth pointing out. You can't use a function to set the value of a constant.

Showing a list of images using PHP

I have got a directory which contains several folders with images. What I want to do is to read all the images using php script and showing them with div in a browser. The code I have tried to implement is the following:
<?php
function listFolderFiles($dir)
{
echo '<ol>';
foreach (new DirectoryIterator($dir) as $folder) {
if (!$folder->isDot()) {
// echo '<li>' . $folder->getFilename() . '<br>';
if ($folder->isDir()) {
$user = $dir . '/' . $folder;
foreach (new DirectoryIterator($user) as $file) {
if ($file != '.' && $file != '..') {
// echo '<li>' . $file->getFilename();
$image = $user . '/' . $file;
echo $image . '<br>';
echo '<div>';
echo '<img src="' . $image . '" width="500" height="500" alt="';
echo '"/>';
echo '</div>';
}
}
}
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles('images');
The command echo $image.'<br>'; prints the names of all images. With the following command I manage to create div, however without the images been displayed inside them:
echo '<div>';
echo '<img src="' . $image . '" width="500" height="500" alt="';
echo '"/>';
echo '</div>';
Am I doing something wrong? The $image paths are correct.
EDIT: I move the folder of images in the same folder with the php file in the wamp folder. Now for example the image file is the following images/01virarias/1_.jpg. I change the call of my function as listFolderFiles('images');. However I am getting the same empty divs.
You need to use an URL. It seems you are using local files. Your browser is the 'problem'. Although it is a matter of security.
The images should be in your server environment.
And it seems you forgot a <li> start tag.
if your path is correct do a inspect element on your browser i think it's quoting issue try
echo '<img src="'. $image .'" width="500" height="500" alt=""/>';
You can use images path relative/absolute like
relative :- ../images/your_image
absolute :- http://localhost/your_image_path

Thumbs are not being displayed even image is there on the location in codeigniter

I'm creating a gallery module, using CodeIgniter the upload functionality is working fine like I can find the images where they should be but I am unable to render them at the same time and the thumbnails are also appearing at the same time. This is the situation:
Here is my model code:
function get_images()
{
$query = $this->db->query("SELECT picAlbumName FROM picAlbum");
foreach($query->result() as $row)
{
$files = scandir($this->gallery_path . '/' . $row->picAlbumName);
$files = array_diff($files, array('.', '..', 'thumbs'));
foreach($files as $file){
$images [] = array (
'url' => $this->gallery_path. '/' . $row->picAlbumName . '/' . $file,
'thumb_url' => $this->gallery_path. '/' . $row->picAlbumName . '/' . 'thumbs/'. $file
);
}
}
print_r($images);
return $images;
}
Controller:
$data['images'] = $this->album_model->get_images();
$this->load->view('album_view' , $data);
view:
<?php if (isset($images) && count($images)):
foreach($images as $images): ?>
<div class="thumb">
<a href="<?php echo $images['url']; ?>">
<img src="<?php echo $images['thumb_url']; ?>" />
</a>
</div>
<?php endforeach; else: ?>
<div id="blank_gallery">Please upload an image</div>
<?php endif; ?>
Pictures are there in the folder, ive checked it twice. Kindly identify the mistake.
Check the 'url' and 'thumb_url' value of images array.
Also add your site's base url before image thumb_url.
I've find out the mistake actually i was not providing base_url() instead of it i was providing realpath().

How to print ñ on img source in php

I have images inside a folder that contains 'ñ' char like Navideño-4.jpg.
I created a script to print all images but if image has ñ char it just prints Navide%C3%B1o-4.jpg
Here is script:
function show($path){
$files = glob($path.'*');
natcasesort($files);
foreach($files as $file) {
echo ' <br/> <img src="' . $file . '" /> <br/>';
}
}
and call it like:
show('images/navidad/');
How can I show ñ char inside img source in php, I have tried urlencode,urldecode..
But had no success
Try:
header('Content-Type: text/html; charset=utf-8');
$test="Navideño-4.jpg";
echo htmlspecialchars( $test );
You may try
$test = "Navideño-4.jpg";
echo utf8_decode($test);
or
$test = utf8_encode("Navideño-4.jpg");
echo utf8_decode($test);
You can use pathinfo and urlencode to display the image.
function show($path){
$files = glob($path.'*');
natcasesort($files);
foreach($files as $file) {
$path_parts = pathinfo($file);
echo ' <br/> <img src="' . $path_parts['dirname'] . '/' . urlencode($path_parts['basename']) . '" /> <br/>';
}
}
You can use utf8_decode
echo ' <br/> <img src="' . utf8_decode($file) . '" /> <br/>';
I just tried your code with php version 5.4.16 and it was working fine for me.
I think its your php version issue. Please check your php version with phpinfo()

php code to display images from directory not working

i have this code to display images, where each user has his own, i'll comment it to save your time
<?php
session_start();
$name=$_SESSION['valid_user']; //saved current username in variable
$loc="./uploads/"; //location of image directory
$path=$loc.$name; //current user's folder to save his images
echo $path."<br>"; //i used this to make sure the path is ok, only for testing
if(is_dir($path)) //if directory exists, show it exists,otherwise show it
{ //doesnt exists
echo "<br>exists";
}
else
{
echo "<br>not exists";
}
$files = glob($path."/");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i]; //picture number
print $num."<br />";
echo '<img src="'.$path.'" alt="random image" height="100" width="100"/>'."<br /><br />";
} //shows the picture till the last one
?>
the output that i get is this this
./uploads/user_name
exists
but it does not show the images, even though the folder is not empty (upload script works fine).
EDIT; solved it (low rep, cant answer my own question).
got it. For anyone who cares, this line here
echo '<img src="' . $path . '/' . $files[$i] . '" <!-- etc --> />';
wasn't working because i added $files, which already contained the path, and it was giving input to img src as
/uploads/username/uploads/username
so that was two times the same path.Upon removing $path, and using just
<img src="' . $files[$i] . '"
did the trick. Thank you all for your help.
I think you need to pass a wildcard path to glob: glob($path . '/*'). You are also not printing the filename in the image source attribute:
echo '<img src="' . $path . '/' . $files[$i] . '" <!-- etc --> />';
Also, your $num is actually the filename, not the picture number - that is $i. You could really simplify that loop using the foreach construct:
foreach($files as $filename) {
// etc
}
you need to add a pattern for using glob afaik
$files = glob($path."/*.*"); // all files
$files = glob($path."/*.jpg"); // all jpgs etc.pp
foreach($files as $idx => $file)
{
$num = $idx+1; //idx starts with 0 so we add one here
print $num."<br />";
echo '<img src="'.$path.'/'.$file'" alt="random image" height="100" width="100"/>'."<br /><br />";
}

Categories