Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Posts
    650
    Rep Power
    20

    Retrieve image for database?


    hi there i found this script that works good for uploading images in a database.the only thing is i don't know how to retrieve it?

    [php]
    <html>
    <head><title>Store binary data into SQL Database</title></head>
    <body>

    <?php
    // code that will be executed if the form has been submitted:


    if ((isset($_POST['submit']))&&(isset($_FILES['form_data']))) {
    upload();
    } else {
    show_form();
    }//end if

    function upload() {
    //includes
    require("conn.php");

    //define variables
    $image_file = '';
    $image_desc = '';
    $type = '';
    $size = 0;
    $name = '';

    $image_file = $_FILES['form_data']['tmp_name'];
    $image_desc = $_POST['form_description'];
    $type = $_FILES['form_data']['type'];
    $size = $_FILES['form_data']['size'];
    $name = $_FILES['form_data']['name'];

    $data = fread(fopen($image_file, "r"), filesize($image_file));
    $data = addslashes($data);
    $result = mysql_query("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".
    "VALUES ('$image_desc','$data','$name','$size','$type')");

    $id= mysql_insert_id();
    print "<p>This file has the following Database ID: <b>$id</b>";

    //show the form again to load the next image
    show_form();
    }

    function show_form()
    {
    // else show the form to submit new data:
    ?>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
    File Description:<br>
    <input type="text" name="form_description" size="40">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
    <br>File to upload/store in database:<br>
    <input type="file" name="form_data" size="40">
    <p>
    <input type="submit" name="submit" value="submit">
    <input type="button" name="move" value="Show" onClick="javascript:window.location='show_desc.php';">
    </p>
    </form>

    <?php
    }
    ?>
    </body>
    </html>
    PHP Code:
    //my retrieve code

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <? echo "<table border='1'>";
    echo 
    "<tr> <th>Name</th> <th>numero</th> <th>pic</th> </tr>";
    // Connects to your Database
    mysql_connect("localhost""techker_techker""techker") or die(mysql_error()) ;
    mysql_select_db("techker_cars") or die(mysql_error()) ;
    //Retrieves data from MySQL
    $data mysql_query("SELECT * FROM binary_data") or die(mysql_error());

    // keeps getting the next row until there are no more to get
    while($info mysql_fetch_array$data )) {
        
    // Print out the contents of each row into a table
        
    echo "<tr><td>"
        echo 
    $info['id'];
        echo 
    "</td><td>"
        echo 
    $info['description'];
        echo 
    "</td><td>";
        echo 
    "</td><td>"
        echo 
    $info['bin_data'];
        echo 
    "</td><td>"
        echo 
    $info['filename']; 
        echo 
    $info['filesize'];
        echo 
    "</td><td>"
        echo 
    "</td></tr>";
        


    echo 
    "</table>";
    ?><a href="deletert.php?image=<?php echo $info['id'?>">Delete</a>
    <td>&nbsp;</td>
    </body>
    </html>
  2. #2
  3. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2006
    Location
    Hindustan
    Posts
    1,121
    Rep Power
    0
    Hi,

    You need to pass headers for the type of image
    Try this:

    PHP Code:
    <?php
        
    echo "</td><td>"
        
    header("Content-type: image/jpeg");
        echo 
    $info['bin_data'];
        echo 
    "</td><td>";  ?>
    HTH
  4. #3
  5. Web Developer/Musician
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Nov 2004
    Location
    Tennessee Mountains
    Posts
    2,424
    Rep Power
    1035
    Originally Posted by tbankar
    Hi,

    You need to pass headers for the type of image
    Try this:

    PHP Code:
    <?php
        
    echo "</td><td>"
        
    header("Content-type: image/jpeg");
        echo 
    $info['bin_data'];
        echo 
    "</td><td>";  ?>
    HTH
    That would cause the document to have an identity crisis. It would not know whether it was an image or an HTML document. You can't mix the two (well technically you can, but data URI techniques aren't for beginners).

    Storing image data in a database is a bad idea. It bloats your database and slows performance. The only reason to store anything other than text and numerical data in a database is to take advantage of the security model. If you were storing sensitive medical information in PDF form (or X ray scans), you might do something like that, otherwise it's not advisable.

    There is no security in your script at all. You need to at least determine if a file is too large, if not restrict to a specific file type. You also do no checks whatsoever to determine that MySQL functions have worked successfully.

    You can use a select statement to retrieve the image data, but you can't display it in a single document. Image data goes in image files, text data goes in text files (including HTML). Mixing the two isn't going to work.
    Last edited by Hammer65; March 4th, 2008 at 09:50 AM.
    Coder Central Tutorials, news and information for the programming community at large.
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Posts
    650
    Rep Power
    20
    you see i had a script that uploads keeps info of the pic but moves it to a folder.it's good but when i made a delete button it deleted the info in the database but not the pic itself?

    is there a way to use it and have it delete the pic to?


    cause me to i didnt like the idea of storing it in a database..
  8. #5
  9. simpleton
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    denver.colorado.usa
    Posts
    392
    Rep Power
    235
    Originally Posted by techker
    you see i had a script that uploads keeps info of the pic but moves it to a folder.it's good but when i made a delete button it deleted the info in the database but not the pic itself?

    is there a way to use it and have it delete the pic to?


    cause me to i didnt like the idea of storing it in a database..
    unlink()
    RTFM!

    Comments on this post

    • Hammer65 agrees : Bah! Too quick for me.
    10% gifted 90% puzzled
  10. #6
  11. Web Developer/Musician
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Nov 2004
    Location
    Tennessee Mountains
    Posts
    2,424
    Rep Power
    1035
    Try unlink.

    Comments on this post

    • bigSeth agrees : You guys beat me to the punch more often than not!
    Coder Central Tutorials, news and information for the programming community at large.
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Posts
    650
    Rep Power
    20
    nice

    ok so this is the script that moves the file in a directory and inserts data in the db

    PHP Code:
    <?php



    $target 
    "car/";
    $target2"car/thumbs/";
    $target3"car/pics/";
    $target $target basename$_FILES['photo']['name']);
    $target2 $target2 basename$_FILES['thumb']['name']);
    $target3 $target3 basename$_FILES['pics']['name']);

    $name=$_POST['name'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $pic=($_FILES['photo']['name']);
    $thumb=($_FILES['thumb']['name']);
    $num=$_POST['numero'];
    $desc=$_POST['description'];
    $pics=($_FILES['pics']['name']);


    // Connects to your Database
    mysql_connect("localhost""techker_techker""techker") or die(mysql_error()) ;
    mysql_select_db("techker_cars") or die(mysql_error()) ;

    //Writes the information to the database
    mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic','$num','$desc','$thumb','$pics')") ;


    if(
    move_uploaded_file($_FILES['photo']['tmp_name'], $target))

    {
    if(
    move_uploaded_file($_FILES['thumb']['tmp_name'], $target2))

    {
    if(
    move_uploaded_file($_FILES['pics']['tmp_name'], $target3))

    {

    echo 
    "The file "basename$_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
    echo 
    "You'll be redirected to Home Page after (4) Seconds";
              echo 
    "<meta http-equiv=Refresh content=4;url=index.php>";
    }
    else {


    echo 
    "Sorry, there was a problem uploading your file.";
    }
    }
    }

    ?>
    this is the deleter page
    PHP Code:
    <?php 
    // database connect script. 
    require('connect.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=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    //Deletes the information to the database
    $delRT "DELETE FROM employees WHERE numero = ".$_GET['numero'].""// Change this to $_GET since we are grabbing a variable from the URL bar 

    $myFile "$_FILES['photo']['name']";  
    unlink($myFile);                             //here i put the unlink




    $dcdc=mysql_query($delRT,$db_link);
    if (!
    $dcdc) {
        
    // Error
        
    echo "Sorry, there was an error deleting your file.";
    } else {
        
    //Tells you if its all ok
    echo "The file "basename$_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
    echo 
    "You'll be redirected to Home Page after (4) Seconds";
              echo 
    "<meta http-equiv=Refresh content=4;url=index.php>";
    }
    ?> 
    <a href="uploader.php">add</a>
    </body>
    </html>
    were i put the unlink i put
    $_FILES['photo']['name'] i think that is the recorded path to the pic?is this ok?
  14. #8
  15. simpleton
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    denver.colorado.usa
    Posts
    392
    Rep Power
    235
    try it. see what happens.
    10% gifted 90% puzzled
  16. #9
  17. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Posts
    650
    Rep Power
    20
    doesn't work..lol

    i think it is because my variable is not in that page?
  18. #10
  19. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Posts
    650
    Rep Power
    20
    ok so i included the file in that file.

    now i have tryed

    <img src=admin/images/".$info['photo'].">

    witch gives me an error cause of the <>

    admin/images/.$info[photo].

    but it gives me an error 2

    now i get that i have to show the path tio the file but how can i identify it?
    $info[photo]
  20. #11
  21. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Posts
    650
    Rep Power
    20
    ok i got it.i made another delete file with

    Code:
    <?php
    
    $fileToRemove = 'file';
    if (file_exists($fileToRemove)) {
       // yes the file does exist
       unlink($fileToRemove);
    } else {
       // the file is not found, do something about it???
    }
    ?>
    the only thing i need is how to pass a variable in
    $fileToRemove = 'file';

    cause i will pass it like my delete file
    .$_GET['numero']

    so it can read the url im passing.
  22. #12
  23. simpleton
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    denver.colorado.usa
    Posts
    392
    Rep Power
    235
    only you know how and where you are keeping the filename. It is YOUR code, right???
    10% gifted 90% puzzled
  24. #13
  25. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Posts
    650
    Rep Power
    20
    ya that part im ok with.but i need info how to to include that code in between the' '

    the only thing i need is how to pass a variable in
    $fileToRemove = 'file';

    cause i will pass it like my delete file
    .$_GET['numero']


    cause i f i put

    $fileToRemove = " .$_GET['photo'].";

    i get
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/techker/public_html/My CAr Script/admin/deleter_pic.php on line 3

    Comments on this post

    • bigSeth disagrees : Seriously?!?!?
  26. #14
  27. simpleton
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    denver.colorado.usa
    Posts
    392
    Rep Power
    235
    PHP Code:
    $fileToRemove $_GET['photo']; 
    If you do not know how to declare a variable STILL...
    10% gifted 90% puzzled
  28. #15
  29. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Posts
    650
    Rep Power
    20
    ha..thx for the help works good.

    i have only been in php for like a couple of mounts
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo