SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    downloading image.

    ok. All I want to do is set up a script so the user can upload an image on their computer the mysql database using a form. And then using a hyperlink on the same page download it to the page in their web site.

    I have created 2 pages. imageform.php: this is where the form is and the script to upload and show which files are available to down load.

    and downloadimage.php: this has the script for downloading the image and is the page where I wish the picture to be downloaded tp it to be downloaded to. Everything works except when I click the hyperlink for one of the pictures it goes to downloadimage.php but nothing is downloaded.

    Below is the script for imageform.php:

    PHP Code:
    <?php include("connect.php");
    if(isset(
    $_POST['submit']) && $_FILES['userfile']['size'] > 0)
    {
    $fileName $_FILES['userfile']['name'];
    $tmpName  $_FILES['userfile']['tmp_name'];
    $fileSize $_FILES['userfile']['size'];
    $fileType $_FILES['userfile']['type'];

    $fp      fopen($tmpName'r');
    $image fread($fpfilesize($tmpName));
    $image addslashes($image);
    fclose($fp);

    if(!
    get_magic_quotes_gpc())
    {
        
    $fileName addslashes($fileName);
    }
    $query1 "INSERT INTO images (name, size, type, image ) ".
    "VALUES ('$fileName', '$fileSize', '$fileType', '$image')";
    $result mysqli_query($connection$query1);
    echo 
    "<br>File $fileName uploaded<br>";
    $query "SELECT id, name FROM images";
    $result mysqli_query($connection$query) or die('Error, query failed');
    if(
    mysqli_num_rows($result) == 0)
    {
    echo 
    "Database is empty <br>";

    else
    {
    while(list(
    $id$name) = mysqli_fetch_array($result))
    {
    ?>
    <a href="downloadimage.php?id=<?php echo $id ;?>"><?php echo $name;?></a></br>
    <?php
    }
    }
    }
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <style type="text/css">
    <!--
    #Layer1 {
        position:absolute;
        width:200px;
        height:115px;
        z-index:1;
    }
    -->
    </style>
    </head>
    <body>
    <form method="post" enctype="multipart/form-data">
    <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr> 
    <td width="246">
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
    <input name="userfile" type="file" id="userfile"> 
    </td></tr>
    <tr>
    <td style="padding-top:10px;" width="80"><input name="submit" type="submit" class="box" id="upload" value=" Upload "></td>
    </tr>
    </table>
    </form>

    </body>
    </html>
    and the script for downloadimage.php:

    PHP Code:
    <?php include("connect.php");
    if(isset(
    $_GET['id'])) 
    {
    // if id is set then get the file with the id from database
    $id  $_GET['id'];
    $query "SELECT name, type, size, image FROM images WHERE id = '$id'";
    $result mysqli_query($connection$query) or die('Error, query failed');
    list(
    $name$type$size$image) = mysqli_fetch_array($result);
    header("Content-length: $size");
    header("Content-type: $type");
    header("Content-Disposition: attachment; filename=$name");
    echo 
    $image;
    exit;
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <div></div>
    </body>
    </html>
    I would appreciate help with this if anyone has any ideas why it does not download

    cass27

  2. #2
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why are you using this approach. Why not retrieve the name and create an HTML link?

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eruna View Post
    Why are you using this approach. Why not retrieve the name and create an HTML link?
    Thats exactly what the OP is doing...
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    why not just insert the name and path into the html?

    $img=msql_fetch_array($result);

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

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

    <title>Untitled Document</title>

    </head>

    <body>

    <div><img src="<?=$img[name]?>"></div>

    </body>

    </html>

  5. #5
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,367
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    echo $image; is just displaying the image name you need echo "<img src=\"$image\">";
    This is assuming that image contains not just the image name but the path to the image.
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

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

    <title>Untitled Document</title>

    </head>

    <body>

    <div>
    <?php include("connect.php");

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

    {

    // if id is set then get the file with the id from database

    $id  $_GET['id'];

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

    $result mysqli_query($connection$query) or die('Error, query failed');

    list(
    $name$type$size$image) = mysqli_fetch_array($result);

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

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

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

    echo 
    "<img src=\"$image\">";


    exit;

    }

    ?>
    </div>

    </body>
    NOTE:

    I think you will get a header error with the posted code but you can sort that out.
    Why do you have this anyway?
    header("Content-length: $size");

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

    header("Content-Disposition: attachment; filename=$name");
    Last edited by Rubble; May 27, 2009 at 08:53. Reason: Added note:

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    From an earlier post, the OP is saving blob data in the image column Rubble.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •