SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: PHP and images

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP and images

    I've been having a bit of trouble trying to get images to display on my site, I can't figure what kinda of method to use... I want to store the image file paths in the database and the images themselves in the file system, but I dunno how to go about and doing it. (FYI I just started learning PHP, so I'm a bit of a newbie)

  2. #2
    SitePoint Mentor bronze trophy
    chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,041
    Mentioned
    9 Post(s)
    Tagged
    1 Thread(s)
    Have a read of the following article http://phpmaster.com/file-uploads-wi...28PHPMaster%29
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see where it mentions how to display the image onto the site, from what I see it just shows how to upload the images to the specified folder, I already have images in the folder, but now I want to display them onto the site.

  4. #4
    SitePoint Addict MikesBarto2002's Avatar
    Join Date
    May 2006
    Location
    New York City
    Posts
    315
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you show us the code that you have so far? It's kind of hard to show you how unless we can see what you have so far.
    James Web Development | New York, NY
    Design, Develop, Deliver

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I'm not sure exactly how to start it, but for right now, I'm basically using the code from the site that chris.upjohn mentioned. but all that does is upload the file as far as I know.

  6. #6
    SitePoint Addict MikesBarto2002's Avatar
    Join Date
    May 2006
    Location
    New York City
    Posts
    315
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You basically will use the upload directory plus the name variable within the while loop to create the path to the image.

    PHP Code:
    $pathtoimage UPLOAD_DIR $parts["filename"] . "-" $i "." $parts["extension"]; 
    echo 
    "<img src='" $pathtoimage "' />"
    UPLOAD_DIR is your constant that points you to the correct folder, and the rest creates the file's name and extension.
    James Web Development | New York, NY
    Design, Develop, Deliver

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the filename part in the code you specified the place where I put the file path for the image?

    Edit: It seems all of this is just flat out confusing for me, i guess I went into something too advanced, since I've only been working with PHP for a couple of months now, is there a guide for complete newbies like me to help me to gradually understand these advanced things? Of course, I can easily do the simple codes like the hello world and stuff like that, but going into the stuff like file uploading, and others, seem too much for me right now.

  8. #8
    SitePoint Mentor bronze trophy
    chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,041
    Mentioned
    9 Post(s)
    Tagged
    1 Thread(s)
    Take your pick http://products.sitepoint.com/?tag=&filters[tag]=php&filters[difficulty]=&simpleform_submit_marker=showme

    EDIT: The URL keeps screwing up so you will just need to copy it.
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the link, and I'm sorry that I couldn't understand what you all were saying.

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. have you reached the point where you are able to upload the images and store their names/paths in the database table?
    2. have you reached the point where you can list the information in your database table?

  11. #11
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't see where it mentions how to display the image onto the site, from what I see it just shows how to upload the images to the specified folder, I already have images in the folder, but now I want to display them onto the site.
    Use the script below and let us know if this is what you are looking for:
    PHP Code:
    <?php
       $dir 
    "images/"// put the name of your images folder between "", example: my_folder/my_images/
       
    $images glob("$dir*{jpg,png,gif}"GLOB_BRACE); // put a list of image types between {} 
       
    foreach ($images as $img)
       {
           echo 
    "<img src='$img' alt='Unable to show $img'/><br/>";
       }   
    ?>

  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well luckily, thanks to a tutorial on youtube, I got it worked out, now I'm trying to get the the image source tag to become a variable, but I can't get it right, and here's the code:
    PHP Code:
    foreach ($dir_contents as $file) {
        
    $file_type strtolower(end(explode('.'$file)));
        
        if (
    $file !== '.' && $file !== '..' && in_array($file_type$file_display) == true) {
            echo 
    '<img src="'$dir'/'$file'" alt="'$file'" width="80px;" height="100px" />';
            }
        }
    }

    $sql "SELECT * FROM phpbb_anime";
    $result $db->sql_query($sql);
    $row $db->sql_fetchrow($result);
    $db->sql_freeresult($result);

    $name $row['name'];
    $desc $row['description'];

    $template->assign_vars(array(
        
    'ANIME_NAME'           => $name,
        
    'ANIME_DESCRIPTION' => $desc,
        
    'ANIME_PIC'            => '<img src="'$dir'/'$file'" alt="'$file'" width="80px;" height="100px" />',
       )
    ); 
    The way it is currently, the image will just display out on the page with no formatting, and I'm working with phpbb, so I'm making it a variable like everything else.

  13. #13
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I couldn't edit my above post so I had to double post, sorry, but anyway, problem solved. I just changed the echo portion to this
    PHP Code:
    $animeimg "<img src='" $dir "/" $file "' alt='" $file "' width='80px;' height='100px' />"
    and then I just included that variable with all the others; with it like that, it worked beautifully!

Bookmarks

Posting Permissions

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