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

  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

  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

  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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.50884 seconds
  • Memory Usage 3,067KB
  • Queries Executed 15 (?)
More Information
Template Usage (36):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (15)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (15)postbit
  • (15)postbit_onlinestatus
  • (15)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (75):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates