Can't read images in from directory using PHP
I've spent a day and a half attempting to read images from a folder and then printing them to a webpage. I have used code provided at a few different reputable websites. They have not worked.
I've used glob, opendir, and scandir to read the folders.
What I am seeing:
1. The user drops through the loop once.
2. The variable used for the image does not print a value.
My belief is that the code is not in any of these instances seeing an image. That has led me to think that I am pointing to the wrong folder. This is a WordPress install located in a subdirectory: http://domainname/subdirectory/wordp...ontent/uploads . The images are in the uploads folder.
The directory path I have used is $dir = /trophy/wp-content/uploads.
Any and all help is appreciated.
Could you post your code and the path of the script?
The code
$current_dir = 'domainname/trophy/wp-content/uploads/';
echo $current_dir;
$dir = opendir($current_dir);
while ($file = readdir($dir))
{
$parts = explode(".", $file);
if (is_array($parts) && count($parts) > 1)
{
$extension = end($parts);
if ($extension == "ext")
{
echo 'hello';
}
}
closedir($dir);
This is another version:
$dir = '/trophy/wp-content/uploads';
$scan = scandir($dir);
for ($i=0; $i<count($scan); $i++)
{
echo '<img src="'.$dir.'/'.$scan[$i].'">';
echo '<p>'.$scan[$i];
if (strpos($scan[$i], 'cat') !== false)
{
echo '<div id="#catalog-directory-images">';
echo '<img src="'.$page_url.$dir.$scan[$i].'" alt="'.$scan[$i].'" />';
echo '</div>';
}
}
PHP Code:
function GetImages ( $dir , array $extensions , $prefix = "" )
{
$images = array();
$it = new RecursiveIteratorIterator (new RecursiveDirectoryIterator ( $dir , FilesystemIterator :: SKIP_DOTS ));
while( $it -> valid ())
{
if (( $pos = strrpos ( $it -> getSubPathName (), '.' )) !== false &&
in_array ( substr ( $it -> getSubPathName (), $pos + 1 ), $extensions ))
{
if ( $prefix )
$prefix = rtrim ( $prefix , '/' ) . '/' ;
$images [] = $prefix . $it -> getSubPathName ();
}
$it -> next ();
}
return $images ;
}
$dir = dirname ( __FILE__ ) . '/wp-content/uploads' ;
$extensions = array( 'jpg' , 'png' , 'gif' );
$prefix = '/wp-content/uploads' ;
$images = GetImages ( $dir , $extensions , $prefix );
I assume that the script's path is the root dir of wordpress. If not, you should change $dir and $prefix.
Getting White Screen
So I plugged it in exactly as you sent it EXCEPT that I added the subdirectory that the WordPress install is in, so the directory and prefix paths are /trophy/wp-content/uploads.
The result is that when I publish the page, the HTML ends at on the line previous to your code. It's all white from just before "<?php"
Any ideas?
You don't need to change the $dir and $prefix if the script is inside the wordpress root dir. The $images contains an array with all images' paths. You should add a foreach construct to iterate over it.
e.g.
PHP Code:
foreach( $images as $img )
{
echo '<img src="' . $img . '" /><br />' ;
}
Okay, I'll plug that in. Thank you.
Still, why did the HTML stop when it hit the code - not even my footer?
Did you wrap the code in <?php ?> tags?
Yes:
<?php
function GetImages($dir, array $extensions, $prefix = "")
{
$images = array();
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS));
while($it->valid())
{
if (($pos = strrpos($it->getSubPathName(), '.')) !== false &&
in_array(substr($it->getSubPathName(), $pos + 1), $extensions))
{
if ($prefix)
$prefix = rtrim($prefix, '/') . '/';
$images[] = $prefix . $it->getSubPathName();
}
$it->next();
}
return $images;
foreach($images as $img)
{
echo '<img src="' . $img . '" /><br />';
}
}
$dir = dirname(__FILE__) . '/wp-content/uploads';
$extensions = array('jpg', 'png', 'gif');
$prefix = '/wp-content/uploads';
$images = GetImages($dir, $extensions, $prefix);
?>
RecursiveDirectoryIterator throws an UnexpectedValueException if the path cannot be found or is not a directory. I suppose that the dir was not found when you added /trophy. Try this
PHP Code:
function GetImages ( $dir , array $extensions , $prefix = "" )
{
$images = array();
$it = new RecursiveIteratorIterator (new RecursiveDirectoryIterator ( $dir , FilesystemIterator :: SKIP_DOTS ));
while( $it -> valid ())
{
if (( $pos = strrpos ( $it -> getSubPathName (), '.' )) !== false &&
in_array ( substr ( $it -> getSubPathName (), $pos + 1 ), $extensions ))
{
if ( $prefix )
$prefix = rtrim ( $prefix , '/' ) . '/' ;
$images [] = $prefix . $it -> getSubPathName ();
}
$it -> next ();
}
return $images ;
}
$dir = dirname ( __FILE__ ) . '/wp-content/uploads' ;
$extensions = array( 'jpg' , 'png' , 'gif' );
$prefix = '/wp-content/uploads' ;
try {
$images = GetImages ( $dir , $extensions , $prefix );
foreach( $images as $img )
echo '<img src="' . $img . '" /><br />' ;
} catch ( UnexpectedValueException $e ) {
die( $e -> getMessage ());
}
ps. You put foreach after the return statement, so it could not be executed.
I put this into my page, still only white where this code begins.
This is the code from the last line to print to the footer:
<!-- LARGE IMAGES AND LINKS -->
<?php
function GetImages($dir, array $extensions, $prefix = "")
{
$images = array();
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS));
while($it->valid())
{
if (($pos = strrpos($it->getSubPathName(), '.')) !== false &&
in_array(substr($it->getSubPathName(), $pos + 1), $extensions))
{
if ($prefix)
$prefix = rtrim($prefix, '/') . '/';
$images[] = $prefix . $it->getSubPathName();
}
$it->next();
}
return $images;
}
$dir = dirname(__FILE__) . '/wp-content/uploads';
$extensions = array('jpg', 'png', 'gif');
$prefix = '/wp-content/uploads';
try {
$images = GetImages($dir, $extensions, $prefix);
foreach($images as $img)
echo '<img src="' . $img . '" /><br />';
} catch (UnexpectedValueException $e) {
die($e->getMessage());
}
?>
</div><!-- ## catalog-collection ## -->
</div><!-- ## text catalog ## -->
<?php endwhile; ?>
<?php get_footer(''); ?>
You should check the log files for error messages.
Btw, I made some small changes to the code.
PHP Code:
function GetImages ( $dir , array $extensions , $prefix = '' )
{
$images = array();
$it = new RecursiveIteratorIterator
(
new RecursiveDirectoryIterator ( $dir , FilesystemIterator :: SKIP_DOTS | FilesystemIterator :: UNIX_PATHS ),
RecursiveIteratorIterator :: LEAVES_ONLY ,
RecursiveIteratorIterator :: CATCH_GET_CHILD
);
while( $it -> valid ())
{
if (( $pos = strrpos ( $it -> getSubPathName (), '.' )) !== false &&
in_array ( substr ( $it -> getSubPathName (), $pos + 1 ), $extensions ))
{
if ( $prefix )
$prefix = rtrim ( $prefix , '/' ) . '/' ;
$images [] = $prefix . $it -> getSubPathName ();
}
$it -> next ();
}
return $images ;
}
$dir = dirname ( __FILE__ ) . '/wp-content/uploads' ;
$extensions = array( 'jpg' , 'png' , 'gif' );
$prefix = '/wp-content/uploads' ;
try {
$images = GetImages ( $dir , $extensions , $prefix );
foreach( $images as $img )
echo '<img src="' . $img . '" /><br />' . "\n" ;
} catch ( UnexpectedValueException $e ) {
die( $e -> getMessage ());
}
I'm running PHP 5.2, and iterator was introduced in 5.3. Won't work.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks