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 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php image display

    hey, i have a small problem which im sure is very simple to work out, i just cant seem to figure it out! i have a page which can let you upload an image to a mysql database.

    at the moment my code is displaying a link which you can download the image from, buy using :


    <a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>


    but i want to be able to view the image in the browser instead of having a link, here is my code i have:


    <?

    if(isset($_GET['id']))

    {

    include 'config.php';

    include 'opendb.php';



    $id = $_GET['id'];

    $query = "SELECT name, type, size FROM upload WHERE id = '$id'";

    $result = mysql_query($query) or die('Error, query failed');

    list($name, $type, $size) = mysql_fetch_array($result);



    header("Content-Disposition: attachment; filename=$name");

    header("Content-length: $size");

    header("Content-type: $type");

    echo $content;



    include 'closedb.php';

    exit;

    }



    ?>

    <html>

    <head>

    <title>Download File From MySQL</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    </head>



    <body>

    <?

    include 'config.php';

    include 'opendb.php';



    $query = "SELECT id, name, customer FROM upload";

    $result = mysql_query($query) or die('Error, query failed');

    if(mysql_num_rows($result) == 0)

    {

    echo "Database is empty <br>";

    }

    else

    {

    while(list($id, $name, $customer) = mysql_fetch_array($result))

    {
    echo "<td>" . ($customer) . "</td><br />";

    ?>

    <a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>

    <?

    }


    }

    include 'closedb.php';

    ?>

    </body>

    </html>


    if anyone could help me i would appreciate it so much!! thanks
    David

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    <img src="<?php echo $name;?>" />
    Assuming $name gets the name of the image-file(including extension).
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Mar 2008
    Location
    Lakeland, FL
    Posts
    39
    Thanks
    1
    Thanked 3 Times in 3 Posts
    remove the Content-Disposition header and it will display instead of force download.

  • #4
    New Coder
    Join Date
    Mar 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cheers guys for both your help!!! i have now removed the header with forced the download, so now when you click on the text link the image displays straight away.

    i have a couple more questions that if you could answer would be great, i was wondering how to have a small thumbnail image you can click on instead of the text link??

    and also is there any way that when you click the link at the moment the image is displayed in another browser window???

    any help would again me much appreciated, and thatnks again for your help already!!

    David

  • #5
    Regular Coder
    Join Date
    Mar 2008
    Posts
    118
    Thanks
    3
    Thanked 9 Times in 9 Posts
    In order to get a thumbnail image and link from it, you have to set the height and width of the image, and wrap the link around it...

    Code:
    <a href="download.php?id=<?=$id;?>">
    <img height="80" width="125" src="<?php echo $name;?>" />
    </a>
    You can set 125 and 80 to anything you want for a thumbnail size..

    As for your second question, if you insert a
    Code:
    target="_blank"
    into the link tag, it will open in a new window.

    Code:
    <a href="download.php?id=<?=$id;?>" target="_blank">
    <img height="80" width="125" src="<?php echo $name;?>" />
    </a>

  • #6
    New Coder
    Join Date
    Mar 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you very much for your response, that code is bringing up thumbnails, but when i click on one of the thumbnails a dialog box pops up saying "you have chosen to open download.php". the odd thing is though this doesnt happen all the time, sometimes the image comes up in a seperate window like i want it to.

    also when i refresh the page on which the thumbnails are on it also pops up with a dialog box saying "you have chosen to open download.php. i just cant figure out which bit of code is making it do this, i think its is something to do with the headers but i do not know what to do to change it.

    any help on this issue i have again would be very helpful, thanks again!

    David

  • #7
    Regular Coder
    Join Date
    Mar 2008
    Posts
    118
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Are you sure you got rid of the Content-Disposition header?

    Seems like it could be a problem.

  • #8
    New Coder
    Join Date
    Mar 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah i have got rid of the content-disposition header, this problem only happens in firefox and someone told me that the dialog box poping up was just an error with firefox, is this anything you have come accross??maybe this is the reason?? thanks

    David


  •  

    Posting Permissions

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