473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Fetching image from MySQL database results in blank / invalid image file.

1
Hello, I have a big problem. I have the following code but it just doesn't want to display my picture, it displays a small area without a picture, submitting and deleteng the image is working, but displaying...DOESN'T :(

//2.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // database connection
  4. $conn = mysql_connect("localhost", "root", "triadpass") OR DIE (mysql_error());
  5. @mysql_select_db ("Books", $conn) OR DIE (mysql_error());
  6.  
  7. // Do this process if user has browse the file and click the submit button
  8. if ($_FILES) {
  9. $image_types = Array ("image/bmp",
  10. "image/jpeg",
  11. "image/pjpeg",
  12. "image/gif",
  13. "image/x-png");
  14.  
  15. $userfile = addslashes (fread (fopen ($_FILES["userfile"]["tmp_name"], "r"), filesize ($_FILES["userfile"]["tmp_name"])));
  16. $file_name = $_FILES["userfile"]["name"];
  17. $file_size = $_FILES["userfile"]["size"];
  18. $file_type = $_FILES["userfile"]["type"];
  19.  
  20. if (in_array (strtolower ($file_type), $image_types)) {
  21. $sql = "INSERT INTO image (image_type, image_data, image_size, image_name, image_date) ";
  22. $sql.= "VALUES (";
  23. $sql.= "'{$file_type}', '{$userfile}', '{$file_size}', '{$file_name}', NOW())";
  24. @mysql_query ($sql, $conn);
  25. Header("Location:".$_SERVER["PHP_SELF"]);
  26. exit();
  27. }
  28. }
  29.  
  30. // Do this process of user has click a file name to view or remove
  31. if ($_GET) {
  32. $iid = $_GET["iid"];
  33. $act = $_GET["act"];
  34. switch ($act) {
  35. case rem:
  36. $sql = "DELETE FROM image WHERE image_id=$iid";
  37. @mysql_query ($sql, $conn);
  38. Header("Location:./2.php");
  39. exit();
  40. break;
  41. default:
  42. echo "<img src=\"immage.php?iid=$iid\">";
  43.  
  44. break;
  45. }
  46. }
  47.  
  48. ?>
  49. <html>
  50. <head>
  51. <title>Storing Images in DB</title>
  52. </head>
  53. <body>
  54. <form method="post" enctype="multipart/form-data">
  55. Select Image File: <input type="file" name="userfile" size="40"><input type="submit" value="submit">
  56. </form>
  57. <?php
  58. $sql = "SELECT * FROM image ORDER BY image_date DESC";
  59. $result = mysql_query ($sql, $conn);
  60. if (mysql_num_rows($result)>0) {
  61. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  62. $i++;
  63. $str .= $i.". ";
  64. $str .= "<a href=\"2.php?iid=".$row["image_id"]."\">".$row["image_name"]."</a> ";
  65. $str .= "[".$row["image_date"]."] ";
  66. $str .= "[".$row["image_size"]."] ";
  67. $str .= "[<a href=\"2.php?act=rem&iid=".$row["image_id"]."\">Remove</a>]<br>";
  68. }
  69. print $str;
  70. }
  71. ?>
  72. </body>
  73. </html>
  74.  
[Please use CODE tags when posting source code. Thanks! --pbmods]

//immage.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // database connection
  4. $conn = mysql_connect("localhost", "root", "triadpass") OR DIE (mysql_error());
  5. $mysql_select_db ("Books", $conn) OR DIE (mysql_error());
  6. $sql = "SELECT * FROM image WHERE image_id=".$_GET["iid"];
  7. $result = mysql_query ($sql, $conn);
  8. if (mysql_num_rows ($result)>0) {
  9. $row = @mysql_fetch_array ($result);
  10. $image_type = $row["image_type"];
  11. $image = $row["image_data"];
  12. Header ("Content-type: $image_type");
  13. print $image;
  14. }
  15. ?>
Jun 17 '07 #1
1 2280
pbmods
5,821 Expert 4TB
Split from original thread, merged duplicate posts.
Jun 17 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Alper Adatoz | last post by:
Hi, i have a little problem. i hope u guys give me a clear solution (: db: mssql i just want to put jpeg file to the image field at the mssql db. and after that i want to call it back..
3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
7
by: theonlydrayk | last post by:
the script that show image is : <?php include('dbinfo.inc.php'); mysql_connect($localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query =...
8
by: William Drew | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.databases.mysql This is an invitation to discuss the following proposal to create newsgroup comp.databases.mysql. Please note that YOU...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
10
by: John Smith | last post by:
I know that uploading an image to a database has been covered, oh, about 3 trillion times. However, I haven't found anything covering uploading to a MySQL database with .net. Please don't...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
3
by: josh.kuo | last post by:
Sorry about the subject, I can't think of a better one. I recently wrote some PHP classes that I think might be of interest to this group. Since I have been reaping the benefits of reading news...
1
by: mpar612 | last post by:
Hi everyone, I'm not sure if this is asking too much or not. I am trying to get the following PHP code to work on my website. It utilizes PHP 5, MySQL 4.1 and the PEAR DB module. I am...
4
by: dac | last post by:
I am quietly going insane on this project. I've never worked on a project like this one before. All my previous sticky forms were for data entry, not editing. I don't know how to display the form...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.