SitePoint Sponsor

User Tag List

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

    downloading image from mysql

    Hi,

    Im relatively new to php. I have just learnt to upload images to the mysql database. now I want to download the image to a specified web page.
    The overall goal is this: The user goes to the form on my web site, browses for his file and uploads it. In the same action the file is then downloaded to the relevant page, and the relevant place on the website. Im not sure where and how I could adapt the script (below) so that as soon as the user uploads they can see it on their page. I am using mediumblob.

    This is the script I have for uploading:

    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);
    }



    $query "INSERT INTO images (name, size, type, image ) ".
    "VALUES ('$fileName', '$fileSize', '$fileType', '$image')";
    $result mysqli_query($connection$query);


    echo 
    "<br>File $fileName uploaded<br>";

    ?>

     

    <!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>
    <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>
    Any ideas would I would be most grateful

    cass27

  2. #2
    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)
    You don't seem to have any identifier to link the image to your user, without this you're not going to be able to match the image to your user.

    Unless of course, this is the intention? Are you just wanting the user to see the image once uploaded, but not necessarily at later stages?
    @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.

  3. #3
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    You don't seem to have any identifier to link the image to your user, without this you're not going to be able to match the image to your user.

    Unless of course, this is the intention? Are you just wanting the user to see the image once uploaded, but not necessarily at later stages?
    Im not sure what you mean by identifier to the user. Can you explain a little more?
    The user just needs to track the file on his computer using the browse button to upload it. (does not need to see it), Then he clicks the submit button. If he then goes to his web page it is there after being downloaded.

    Does this make sense?

  4. #4
    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)
    Sure, let me elaborate some more.

    The only information you add to the database when you upload an image is name, size, type and image(data).

    Say you have a user, who visits their page after they uploaded an image. How would you find any images that they uploaded?

    Normally you would execute a query similar to:-
    PHP Code:
    <?php
    $sSQL 
    "SELECT id, name FROM images WHERE user_id = 5"# 5 being the users, well, id. :-)
    ?>
    Whereas, in your case, you could not.

    @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.

  5. #5
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Sure, let me elaborate some more.

    The only information you add to the database when you upload an image is name, size, type and image(data).

    Say you have a user, who visits their page after they uploaded an image. How would you find any images that they uploaded?

    Normally you would execute a query similar to:-
    PHP Code:
    <?php
    $sSQL 
    "SELECT id, name FROM images WHERE user_id = 5"# 5 being the users, well, id. :-)
    ?>
    Whereas, in your case, you could not.

    I see. There are two things im unsure of:
    1. Should the above script go on the page in the div where the image should be downloaded to?
    2. As the users will be downloading many pics could I use the identifier in this way:
    WHERE name = $name; so they are actually targeting the actual pic?

    cass27

  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)
    Uploading a side here's a quick example using 2 scripts, viewList.php displays a list of images uploaded by user #5. It, in turn, uses viewImage.php to display the actual image for you.

    viewList.php
    PHP Code:
    <?php
    $sSQL 
    "SELECT id, name FROM images WHERE user_id = 5";
    $rResult mysql_query($sSQL);
    echo 
    '<ul>';
    while(
    $aRecord mysql_fetch_assoc($rResult))
    {
        
    printf(
            
    '<li>
                %s
                <br />
                <img src="viewImage.php?id=%s" alt="%s" />
            </li>'
    ,
            
    $aRecord['name'],
            
    $aRecord['id'],
            
    $aRecord['name']
        );
    }
    echo 
    '</ul>';
    ?>
    viewImage.php
    PHP Code:
    <?php
    if(isset($_GET['id']) && ctype_digit($_GET['id']))
    {
        
    $sSQL sprintf(
            
    "SELECT type, image FROM images WHERE id = %s LIMIT 1",
            
    $_GET['id']
        );
        
    $rResult mysql_query($sSQL);
        
    $aRecord mysql_fetch_assoc($rResult);
        
    header(
            
    sprintf(
                
    'Content-Type: %s',
                
    $aRecord['type']
            )
        );
        echo 
    $aRecord['image'];
        exit;
    }
    ?>
    Hopefully, you should now be able to see the importance of linking the data. We find the list of images uploaded by our user with his/her id number, then we use the id of the image to find out a particular image for viewImage.php to render.

    Without the user_id field, all this would be impossible.
    @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.

  7. #7
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...ok Im getting there.

    But what does %s mean?

    and why in viewlist are there TWO $aRecord = ['name'] ?



    thanks

  8. #8
    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)
    &#37;s are placeholders for values, take a look at the sprintf() documentation. There are 2 $aRecords['name'] in the string as one one is for display and the other for the alt tag.

    ...ok Im getting there.
    More than that I'd say.
    @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.

  9. #9
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its so good when it kind of falls together like a jigsaw. So print() is really the same as echo (which i use a lot). And sprintf is the srtring formatted.



    I think your right I am getting there. I must have a clever teacher

    Thank you.

    cass27


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
  •