www.webdeveloper.com
Results 1 to 9 of 9

Thread: get an image with an unknown file type

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    53

    get an image with an unknown file type

    I'm making a web-app for Conky that's like a Conky script database.

    My problem is: I am putting in a preview image in the sidebar.

    It will be named like: uploads/**script id**.**gif/jpg/bmp/png**

    How do I do this?

  2. #2
    Join Date
    Jan 2010
    Posts
    84
    This is just a quick suggestion. There might be something better or more efficient out there that you could do.

    First, I'd get a list of all the files in the directory, and then search through it for any image file extensions:
    PHP Code:
    <?php
    $files_list 
    "";
    if (
    $handle opendir('.')) {
        while (
    false !== ($file readdir($handle))) {
            if (
    $file != "." && $file != "..") {
                
    $files_list .= $file ",";
            }
        }
        
    closedir($handle);
    }
    ?>
    Not sure if this is exactly what you were looking for.

  3. #3
    Join Date
    Aug 2009
    Posts
    53
    How would I implement this?

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    If I understand the issue, you want to reference a file, you will know the filename, but the extension could be one of a few possibilities. Rather than getting a list of every file, I would be inclined to do it the other way and check the list of extensions. This will presumably be a much shorter list:

    PHP Code:
    <?php

    $exts 
    = array('png''gif''jpg''jpeg');
    $file 'images/hello';

    $src '';
    foreach (
    $exts as $ext) {
        if (
    file_exists("$file.$ext")) {
            
    $src "$file.$ext";
            break;
        }
    }

    printf('<img src="%s" />'$src);

    ?>

  5. #5
    Join Date
    Aug 2009
    Posts
    53
    Didn't work.

    Here's some more info:
    the filename is $id & the filetype will vary. I'm making a thing that uploads an image & is displayed.

  6. #6
    Join Date
    Nov 2008
    Posts
    2,477
    Could you be a little more specific than "didn't work?". I tested this on my server and it worked fine.

  7. #7
    Join Date
    Aug 2009
    Posts
    53
    It didn't show the image.

  8. #8
    Join Date
    Nov 2008
    Posts
    2,477
    Quote Originally Posted by mattwidge View Post
    It didn't show the image.
    If you can't be bothered to post more than a few words, at least detailing some things like the actual output of the script and any debugging you've tried, I can't be bothered to help you fix it. Maybe if you show a bit of courtesy to the people going out of their way to help you, you will find people more willing to keep on helping.

  9. #9
    Join Date
    Jan 2010
    Posts
    84
    I think you'll need to give us some more specific information about what you want to happen.
    The script Mindzai posted will print off links to any images in the images/ directory with the name hello.png, hello.gif, hello.jpg or hello.jpeg. That could be modified slightly to fit what you said originally like this:

    PHP Code:
    <?php

    $exts 
    = array('png''gif''jpg''jpeg');
    $file "uploads/$script_id";         // <-- You'd have to define $script_id

    $src '';
    foreach (
    $exts as $ext) {
        if (
    file_exists("$file.$ext")) {
            
    $src "$file.$ext";
            break;
        }
    }

    printf('<img src="%s" />'$src);

    ?>

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