Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP file to get image files in a directory..

    Code:
    <?php
    
    
    
    $dirname = '.';
    
    $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)"; //valid image extensions 
    
    
    
    if($handle = opendir($dirname)) {
    
    
    
    	while(false !== ($file = readdir($handle))){
    
    		if(eregi($pattern, $file)){ //if this file is a valid image
    
    
    
    			echo '<img src="'.$file.'"/>';
    
    
    
    		}
    
    	}
    
    
    
    	closedir($handle);
    
    
    
    } 
    
    
    
    ?>

    Is the code, currently it works if the images are in the same directory as the php file... however I can't get it to find images in a folder called "images" in the folder that the file is in..

    I have tried changing
    Code:
    $dirname = '.';
    to

    Code:
    $dirname = '/images/';
    among many other things.. any help?

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Are you sure images isn't back a folder? If so you'd have to do ../images

    The best way would be to do the full path, like home/php/user/public_html/images/
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by masterofollies View Post
    Are you sure images isn't back a folder? If so you'd have to do ../images

    The best way would be to do the full path, like home/php/user/public_html/images/


    I have tried using the full path, didn't work
    Tried ../images
    still no luck.

    folder directory is like this..:

    website
    --folder (php file in this folder)
    -----images (images in this folder)


    the images folder is inside the php file's folder.

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    The problem with the full path is when you upload to server, then the path changes and you have to edit file. To go up one folder try:

    $dirname = '../images/';
    or
    $dirname = "../images/";

    It's untested and may need some tweaking.


    EDIT:
    " the images folder is inside the php file's folder."
    Well then $dirname = "images/"; should work, if not try single quotes.


    ------------
    Last edited by Len Whistler; 01-22-2010 at 10:29 PM.
    Leonard Whistler

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Len Whistler View Post
    The problem with the full path is when you upload to server, then the path changes and you have to edit file. To go up one folder try:

    $dirname = '../images/';
    or
    $dirname = "../images/";

    It's untested and may need some tweaking.


    EDIT:

    Well then $dirname = "images/"; should work, if not try single quotes.


    ------------
    tried all your suggestions, still no luck =\

    http://www.freewebsitereview.org/fader/

    Has all your suggestions, each in its seperate div, only one that works is still the $dirname = '.';

  • #6
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    The path to that image is:

    fader/pic12.jpg

    Your image folder is at: fader/images Try:

    $dirname = "fader/images/";
    Leonard Whistler

  • #7
    New Coder
    Join Date
    Jan 2010
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Len Whistler View Post
    The path to that image is:

    fader/pic12.jpg

    Your image folder is at: fader/images Try:

    $dirname = "fader/images/";
    yes I know that's because that is the working one, the one that doesn't see the images folder when
    $dirname is set to '.'

    so yes, that image is fader/pic12.jpg

    however the images folder is fader/images

    $dirname = "fader/images/";
    = no luck

  • #8
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Code:
    website
    --folder (php file in this folder)
    -----images (images in this folder)
    If that folder name is fader, then the correct path to images from that php file is:

    PHP Code:
    $dirname "images/";  // try single quotes if double quotes doesn't work 
    Is the php file and images folder in the same folder?

    -------------
    Last edited by Len Whistler; 01-22-2010 at 10:59 PM.
    Leonard Whistler

  • #9
    New Coder
    Join Date
    Jan 2010
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Len Whistler View Post
    Code:
    website
    --folder (php file in this folder)
    -----images (images in this folder)
    If that folder name is fader, then the correct path to images from that php file is:

    PHP Code:
    $dirname "images/";  // try single quotes if double quotes doesn't work 
    Is the php file and images folder in the same folder?

    -------------
    Nope,

    Website
    --Folder // php file located here
    --Images // images located here

    Screenshot of the structure:




    Here's full code of the php file:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    
    "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <title>testing</title>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <meta name="keywords" content="blackstone-millville, blackstone, millville, regional, school, district, education, high, middle, elementary, massachusetts, melville, milville, bmrsd ">
    
    <meta name="description" content="Welcome to the Blackstone-Millville Regional School District. Located in Blackstone, Massachusetts  U.S.A., we are a K-12 Public School System serving the towns of Blackstone and Millville">
    
    <meta name="distribution" content="global">
    
    <meta name="resource-type" content="document">
    
    <meta name="revisit-after" content="3 days">
    
    
    
    
    
    <style type="text/css">
    
    
    
    .zind {
    
    z-index:1;
    
    }
    
    
    
    body {
    
    	margin-left: 0px;
    
    	margin-top: 0px;
    
    	margin-right: 0px;
    
    	margin-bottom: 0px;
    
    }
    
    body,td,th {
    
    	font-family: Arial, Helvetica, sans-serif;
    
    	font-size: 12px;
    
    	color: #000000;
    
    }
    
    .style1 {font-family: Arial, Helvetica, sans-serif}
    
    -->
    
    </style>
    
    
    
    
    
       <!-- link to magicslideshow.css file -->      
    
            <link rel="stylesheet" type="text/css" href="magicslideshow.css" media="screen"/>      
    
    	<!-- link to magicslideshow.js file -->
    
    	<script src="magicslideshow.js" type="text/javascript"></script>
    
    	<script type="text/javascript">
    
    MagicSlideshow.options = {
    
    'speed':'4',
    
    'effect':'fade',
    
    'effect-next':'fade',
    
    'effect-jump':'fade',
    
    'loop':'yes',
    
    'loop-type':'next',
    
    'text-duration':'1.0'
    
    }
    
    </script>
    
    
    
    
    
    </head>
    
    
    
    <body>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    		  Test
              Test
              Test
    
    		  
    
    		  
    
    		  <div class="MagicSlideshow">
    
    <?php
    
    
    
    $dirname = "images/"; 
    
    $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)"; //valid image extensions 
    
    
    
    if($handle = opendir($dirname)) {
    
    
    
    	while(false !== ($file = readdir($handle))){
    
    		if(eregi($pattern, $file)){ //if this file is a valid image
    
    
    
    			echo '<img src="'.$file.'"/>';
    
    
    
    		}
    
    	}
    
    
    
    	closedir($handle);
    
    
    
    } 
    
    
    
    ?>
    
    </div>
    
    
    
    </body>
    
    </html>

  • #10
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by Len Whistler
    Is the php file and images folder in the same folder?
    Quote Originally Posted by Toups
    Nope,

    According to the screen shot the php file and images folder are in the same folder. The correct path to the images folder from the index.php file is:

    PHP Code:
    $dirname "images/";  // try single quotes if double quotes doesn't work 


    -------
    Leonard Whistler

  • #11
    New Coder
    Join Date
    Jan 2010
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Len Whistler View Post
    According to the screen shot the php file and images folder are in the same folder. The correct path to the images folder from the index.php file is:

    PHP Code:
    $dirname "images/";  // try single quotes if double quotes doesn't work 


    -------
    oh sorry, thought you meant the php file being in the images fodler itself, not them both being in the fader folder.

    none the less,
    $dirname = "images/";
    does not work.

  • #12
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Try small changes such as:

    PHP Code:
    $dirname 'images';

    // or

    $dirname "images"
    Leonard Whistler

  • #13
    New Coder
    Join Date
    Jan 2010
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Len Whistler View Post
    Try small changes such as:

    PHP Code:
    $dirname 'images';

    // or

    $dirname "images"
    still no luck =\

  • #14
    New to the CF scene
    Join Date
    Jul 2012
    Location
    Hyderabad, India
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Well there was a small thing missing in your code.
    As your images are in a folder called "images", you have not specified the directory while calling <img src...> tag.

    So, code should be something like this now:


    <?php
    error_reporting(0);
    $dirname = "images";

    $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)"; //valid image extensions



    if($handle = opendir($dirname)) {



    while(false !== ($file = readdir($handle))){

    if(eregi($pattern, $file)){ //if this file is a valid image


    echo "<img src=\"images/";
    echo $file;
    echo "\" width=\"100\" height=\"100\">";




    }

    }



    closedir($handle);



    }

    ?>


    Enjoy

  • #15
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    this will do what you want:

    PHP Code:
    <?php
    error_reporting
    (0);
    $config = array("images",array('.jpg','.png','.jpeg','.gif'),"100","100");
    if(
    $handle opendir($config[0])){
        while(
    false !== ($file readdir($handle))){
            foreach(
    $config[1] as $key => $type) {
                if(
    stristr($file$type) != FALSE) { 
                    echo 
    "<img src=\"".$config[0]."/".$file."\" width=\"".$config[2]."\" height=\"".$config[3]."\">";
                }
            }
        }
        
    closedir($handle);
    }
    ?>


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •