Images in a database

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Images in a database

    Ok, I had a more barebones version of this script working, but when I
    tried to put it in with my larger script, it broke. I think the problem
    lies somewhere in the uploading, but I'm not sure of this. Anyway,
    here's what I have:

    index.php
    ---------------------------------
    <?
    include("header.inc");
    if (!isset($_SESSION['username']) or ($_SESSION['username'] == "Guest")) {
    header ("Location: http://www.thisisfake.com/index.php");
    }
    dbconnect();
    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <title>Upload</title>
    </head>

    <body>
    <center>
    <?


    if($id) {

    switch ($action) {

    case "delete":
    $data = mysql_query("SELECT id,uploader FROM files WHERE id = '$id'");
    $id = mysql_result($data,0,"id");
    $uploader = mysql_result($data,0,"uploader");
    if ($_SESSION["username"] == $uploader || $_SESSION["level"] ==
    "admin") {
    mysql_query("DELETE FROM files WHERE id = '$id'");
    echo "File deleted";
    echo '<META HTTP-EQUIV="refresh"
    content="2;URL=http://www.thisisfake.com/upload/">';
    } else {echo "Invalid permissions";}
    break;
    default: //Echo contents if image, else send as file download
    $query = "select mimetype, data from files where id = $id";
    $result = mysql_query($query);

    $data = mysql_result($result,0,"data");
    $type = mysql_result($result,0,"mimetype");
    if ($type == "image/pjeg" || $type == "image/jpeg" || $type ==
    "image/x-png" || $type == "image/png" || $type == "image/gif"|| $type ==
    "image/bmp") {
    echo base64_decode($data);
    } else {
    get($id); //This send the file as a download, instead of echoing the
    contents
    }
    break;
    }
    } else {

    // Listing of files and the form

    echo"
    <form method=POST action=upload.php enctype=multipart/form-data>
    <p>File to upload:<br>
    <input type=file name=file>
    <input type='submit' name='submit' value='Upload'>
    </form>
    ";

    //All this stuff works
    echo "<p></p>";
    if ($data = getInfo()) {

    echo '<table border="0" align="center">
    <tr bgcolor="#bad1d1">
    <td>File Name</td>
    <td><center>File Size</center></td>
    <td><center>Mime Type</center></td>
    <td><center>Checksum</center></td>
    <td><center>Extension</center></td>
    <td><center>Uploader</center></td>
    <td><center>Date</center></td>
    <td><center>Option</center></td>
    </tr>
    ';
    for ($i=0; $i<count($data); $i++) {
    echo '
    <tr bgcolor=#CCCCCC>
    <td><a
    href="view.php?id='.$data[$i]["id"].'">'.$data[$i]["file_name"].'</a></td>
    <td>'.$data[$i]["file_size"].'</td>
    <td>'.$data[$i]["mimetype"].'</td>
    <td>'.$data[$i]["checksum"].'</td>
    <td>'.$data[$i]["extension"].'</td>
    <td>'.$data[$i]["uploader"].'</td>
    <td>'.$data[$i]["date"].'</td>';
    if ($_SESSION["username"] == $data[$i]["uploader"] ||
    $_SESSION["level"] == "admin") {
    echo '<td><a
    href="index.php?action=delete&id='.$data[$i]["id"].'">Delete</a></td>';}
    echo '</tr>
    ';
    }
    echo '</table>';
    echo '<br>';
    echo 'Number of files: ';
    echo blobcount();
    echo '<br>';
    }

    }
    ?>
    </center>
    </body>
    </html>




    upload.php
    ----------------------------------------------
    <?
    include('header.inc');
    dbconnect();

    $type = $file_type;
    if ($type == "image/pjeg" || $type == "image/jpeg" || $type ==
    "image/x-png" || $type == "image/png" || $type == "image/gif") {
    $handle = fopen($file,'rb');
    $file_content = fread($handle,filesize($file));
    fclose($handle);
    $encoded = chunk_split(base64_encode($file_content));
    $uploader = $_SESSION['username'];
    $sql = "INSERT INTO files (id, file_name, data, file_size, mimetype,
    extension, checksum, uploader, date) VALUES ('', '".$file_name."',
    '".$encoded."', '".filesize($file)."', '".$file_type."',
    '".getExtension($blob_name)."', '".generate_sfv_checksum($file)."',
    '".$uploader."', NOW())";
    mysql_query($sql);
    } else {
    $handle = fopen($file,'rb');
    $file_content = fread($handle,filesize($file));
    fclose($handle);
    $encoded = chunk_split(base64_encode($file_content));
    $uploader = $_SESSION['username'];
    $sql = "INSERT INTO files (id, file_name, data, file_size, mimetype,
    extension, checksum, uploader, date) VALUES ('', '".$file_name."',
    '".$encoded."', '".filesize($file)."', '".$file_type."',
    '".getExtension($blob_name)."', '".generate_sfv_checksum($file)."',
    '".$uploader."', NOW())";
    mysql_query($sql);
    }
    //}

    header ("Location: http://www.thisisfake.com/upload");

    ?>




    view.php
    ---------------------------------------------------------
    <?
    echo '<img src="http://www.thisisfake.com/upload/index.php?id='.$id.'">';
    ?>







    I may have broken it further after the initial breaking, so if something
    seems doubly broken that might be it. If you need more information just ask.
    Plex Guest

  2. Similar Questions and Discussions

    1. images in database
      Is there a tutorial somewhere that tells/shows how to display an image from a db. I have my image paths in a mysql db and I am using php to display...
    2. Resizing Images From a database
      Hi, can anyone help me? I have a load of images in a database which is linked and displayed to a webpage, but the images are all one size (too...
    3. FLASH & ASP - Database Images
      I have a little problem with displaying asp results in flash, I hope someone can help me. I've managed to display images referenced in a database in...
    4. Images, PDf, etc in a database
      Hi all, We picked up a project were we would need to store all files like Images, PDF, Word files in a database (Oracle or MS SQL). All fine so...
    5. Images from a database
      Hi everyone. I have just discovered how to display an image from a database in the browser....changing the contenttype to "image/jpeg" and then...
  3. #2

    Default Re: Images in a database

    Plex <invalid@thisisfake.com> wrote in
    news:-MCdnUGBcfrQvfvcRVn-tg@comcast.com:

    > I may have broken it further after the initial breaking, so if
    > something seems doubly broken that might be it. If you need more
    > information just ask.

    error messages, buddy, error messages. surely you're not expecting someone
    to debug that line by line??
    Good Man Guest

  4. #3

    Default Re: Images in a database

    Ok, I fixed it myself, all it was was the index.php was outputting html
    regardless of whether there was an image data request, so as a result
    the data send included html in it, which made the image "corrupt".
    Plex Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139