#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Php image viewer


    Hi, I want to create a php script which dynamically displays a given image.
    Let's say I write on my address bar site/viewer.php?image.jpg. I have three sample images on the viewer.php directory. entering any of them after /viewer.php? the scripts should find it and browse it. I'm new in php so this is what I have so far and doesn't work:
    Code:
     <?php
        
        $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        echo $url;
        //================
        //img tag for current image
        $thisImage = '<img class="mainimg" src="' $url '" alt="as" />';
        
        ?>
        <html>
        <head>
        <title></title>
        
        </head>
        <body>
        
        <div><?=$thisImage?></div>
        </body>
        </html>
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,999
    Rep Power
    9397
    Until you understand more about PHP and HTTP, change your URL to be
    Code:
    viewer.php?file=image.jpg
    $_GET["file"] will then be "image.jpg" and you can use that for your IMG.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by requinix
    Until you understand more about PHP and HTTP, change your URL to be
    Code:
    viewer.php?file=image.jpg
    $_GET["file"] will then be "image.jpg" and you can use that for your IMG.
    Thanks for your help! now I know something new
    here is my updated code:

    Code:
        <?php
        //img tag for current image
        $thisImage = '<img class="mainimg" src="'.$_GET["file"].'" alt="'.$_GET["file"].'" />';
        
        ?>
        <html>
        <head>
        <title><?php $_GET["file"]?></title>
        <style type="text/css">
        body{color:#000; background-color:#900000;}
        </style>
        </head>
        <body>
        
        <div style="text-align:center; width:100%;"><?=$thisImage?>
        </div>
        <div class=navigation><? navigationcode ?></div>
        </body>
        </html>
    is there anyway to add some basic navigation to it?
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,999
    Rep Power
    9397
    Navigation like what?

    Also I should point out: printing out $_GET stuff directly isn't safe. You should be doing a couple things to it first.
    PHP Code:
    $thisImage '<img class="mainimg" src="'.htmlentities(urlencode($_GET["file"])).'" alt="'.htmlentities($_GET["file"]).'" />'
    htmlentities() makes sure no one can put HTML tags in the filename and screw up your page, and urlencode() makes sure that if the filename contains something like slashes (which you shouldn't allow) they'll be escaped and the filename will be safe.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by requinix
    Navigation like what?
    Let's say the script can also read directory content(JPG only); so once the given image is displayed you can go to the next or previous one.

    printing out $_GET stuff directly isn't safe.
    If I write domain/viewer.php?file=datawhatever.php I can browse that file or download it?Well, that doesn't make sense since I call the variable inside an IMG tag; but I'm gonna change what you say. I didn't know about the slashes nor that I can permanently change a page content with html tags. I still have a lot to learn. I'm reading a tutorial a page every day to get used to every single thing I learn. Thanks a lot!
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,999
    Rep Power
    9397
    Originally Posted by mlatelcom
    Let's say the script can also read directory content(JPG only); so once the given image is displayed you can go to the next or previous one.
    glob can give you an array of all the files matching a pattern, such as "*.jpg". Find the current picture in that list and you can tell which image is the previous image and which is the next image.

    Originally Posted by mlatelcom
    If I write domain/viewer.php?file=datawhatever.php I can browse that file or download it?
    No, that's the nice thing about your script: all it does is output an IMG tag. If the user altered the URL so that it didn't point to an image, or something they shouldn't access, then only the IMG would be messed up.

    Originally Posted by mlatelcom
    I didn't know about the slashes nor that I can permanently change a page content with html tags. I still have a lot to learn. I'm reading a tutorial a page every day to get used to every single thing I learn. Thanks a lot!
    For better or worse, PHP does a good job of hiding all the "gotchas" with web development. Take them one at a time.

IMN logo majestic logo threadwatch logo seochat tools logo