www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Can't read images in from directory using PHP

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    18

    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.

  2. #2
    Join Date
    Aug 2009
    Posts
    18
    Anyone?

  3. #3
    Join Date
    Apr 2010
    Posts
    88
    Could you post your code and the path of the script?

  4. #4
    Join Date
    Aug 2009
    Posts
    18

    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);

  5. #5
    Join Date
    Aug 2009
    Posts
    18
    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>';
    }
    }

  6. #6
    Join Date
    Apr 2010
    Posts
    88
    PHP Code:
    function GetImages($dir, array $extensions$prefix "")
    {
            
    $images = array();
            
    $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirFilesystemIterator::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.

  7. #7
    Join Date
    Aug 2009
    Posts
    18

    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?

  8. #8
    Join Date
    Apr 2010
    Posts
    88
    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 />';


  9. #9
    Join Date
    Aug 2009
    Posts
    18
    Okay, I'll plug that in. Thank you.

    Still, why did the HTML stop when it hit the code - not even my footer?

  10. #10
    Join Date
    Apr 2010
    Posts
    88
    Did you wrap the code in <?php ?> tags?

  11. #11
    Join Date
    Aug 2009
    Posts
    18
    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);

    ?>

  12. #12
    Join Date
    Apr 2010
    Posts
    88
    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($dirFilesystemIterator::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.

  13. #13
    Join Date
    Aug 2009
    Posts
    18
    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(''); ?>

  14. #14
    Join Date
    Apr 2010
    Posts
    88
    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($dirFilesystemIterator::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());


  15. #15
    Join Date
    Aug 2009
    Posts
    18
    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
  •  
HTML5 Development Center



Recent Articles