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.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post

    Displaying images from folder

    I want this script to display two images and then put the next two on a new line. But for some reason the <"br />" tag has been echo'd a load of times although the reasults are not JPG's...

    PHP Code:
                 //Display images in cars folder
                
    $name strtolower(G_NAME); //Converts name to lowercase as PHP is case sensitive!
                
    $dir "images/cars/".$name$open opendir($dir); //Get directory location and open it
                
    $i=0
                while (
    $file readdir($open)) { //Read files inside folder
                    
    if ($file == false) { echo "Empty!"; }
                    
    $nfile explode(".",$file); //Check that the result is a image (jpg).
                    
    if ($nfile[1] == "jpg" || "JPG") { echo "<img src=\"images/cars/$name/$file\" width=\"50px\">"
                        
    $i++; }
                        if (
    $i <= 2) { echo "<br />"; } //Adds new line after two images
                    

    Result in HTML
    Code:
     <!--Content goes here-->
    Audi
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <img width="50px" src="images/cars/audi/.">
    <br>
    <img width="50px" src="images/cars/audi/image2.JPG">
    <br>
    <img width="50px" src="images/cars/audi/..">
    <img width="50px" src="images/cars/audi/image3.JPG">
    <img width="50px" src="images/cars/audi/image1.JPG">
    <img width="50px" src="images/cars/audi/image4.JPG">        
    </div>
    I have tryed
    PHP Code:
    if (is_file($file)) { echo "blahblah"; } 
    Dunna work, just displays nothing...
    Last edited by martynball; 03-07-2010 at 09:33 PM.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,340
    Thanks
    13
    Thanked 349 Times in 345 Posts
    PHP Code:
     <!--Content goes here-->
    Audi
    <br// $i = 0 <=2 (true)
    <br// $i = 0 <=2 (true)
    <br// etc.
    <br>
    <
    br>
    <
    br>
    <
    br>
    <
    img width="50px" src="images/cars/audi/."// <-- current directory
    <br// $i = 1 <=2 (true)
    <img width="50px" src="images/cars/audi/image2.JPG">
    <
    br// $i = 2 <=2 (true)
    <img width="50px" src="images/cars/audi/.."// <-- parent directory
    <img width="50px" src="images/cars/audi/image3.JPG">
    <
    img width="50px" src="images/cars/audi/image1.JPG">
    <
    img width="50px" src="images/cars/audi/image4.JPG">        
    </
    div
    further, there is no differenciation between files and folders. and you insert a break if $i is less then 3 (which is not what you’d translate into "after every 2 images") you’ll need the modulo operator for that.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    martynball (03-07-2010)

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,340
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by JordanW View Post
    /home/<user>/public_html/images/
    I doubt, that a browser can access /home/<user>. I could imagine that /images/img.jpg could work (given public_html is the web root) or use <base> to make any relative paths absolure.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    martynball (03-07-2010)

  • #4
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    I don't know what the absolute path is. How can I stop the parent directory and the current directory from showing?

    And I will see what happens if I change it to if($i == 2)

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,340
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by martynball View Post
    I don't know what the absolute path is.
    ???

    Quote Originally Posted by martynball View Post
    How can I stop the parent directory and the current directory from showing?
    read in readdir()’s manual entry, there are all kinds of applications described

    Quote Originally Posted by martynball View Post
    And I will see what happens if I change it to if($i == 2)
    exactly 1 break after the second hit. I already said, that you should use the modulo operator (%).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    martynball (03-07-2010)

  • #6
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    So for the modulo thing, do I do this:
    PHP Code:
    if ($i 2) { echo "<br />"; } 

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,340
    Thanks
    13
    Thanked 349 Times in 345 Posts
    you’re not serving XHTML, thus
    PHP Code:
    if ($i 2) { echo "<br>"; } 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Got this from the link you give me:
    PHP Code:
    if ($handle opendir($dir)) {
                    while (
    false !== ($file=readdir($handle))) {
                        if (
    $file != "." && $file != "..") {
                            
    $i++;
                            echo 
    "$file";
                            if (
    $i 2) { echo "<br>"; }
                            }
                        }
                        
    closedir($handle);
                    } 
    Works, cheers And half of the "<br>" tags where caused by another problem, so that is okay. All working now cheers

    I don't understand how this works though, please can you break it down for me?
    PHP Code:
    while (false !== ($file=readdir($handle))) { 
    Last edited by martynball; 03-07-2010 at 10:22 PM.

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,340
    Thanks
    13
    Thanked 349 Times in 345 Posts
    readdir() works similar to (e.g.) mysql_fetch_assoc() it returns a file/dir name as long as there are some left, if not it returns false (which you have explicitly have to test (including a type test))
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #10
    New Coder
    Join Date
    Jan 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    10
    Thanked 0 Times in 0 Posts
    and what I don't see coming back in the answers of empty results:

    PHP Code:
    if ($file != ''
    Just for the people who missed it.

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    This is from another post ...
    You grab only the specific filetypes you want, and it puts them into an array.
    From the array, you can shuffle it, sort it, pick away at it, whatever you want.

    PHP Code:
    <?php

    // directory name where they are stored.
    $dir_name="photos";

    // put these types of images into an array.
    $images array_merge(
    glob("$dir_name/*.jpg"),
    glob("$dir_name/*.png"),
    glob("$dir_name/*.gif")
    );

    // Randomize the array.
    shuffle($images);

    // Display the first 5 random images.
    echo"
    <img src='$images[0]' /><br />
    <img src='$images[1]' /><br />
    <img src='$images[2]' /><br />
    <img src='$images[3]' /><br />
    <img src='$images[4]' /><br />
    "
    ;

    // If you would rather display all of them.
    //foreach($images as &$img){
    //echo "<img src='$img' /><br />";
    //}


  •  

    Posting Permissions

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