-2

Can someone please help as I am at a loss. How can I get an image from MYSQL database onto my webpage. anyname.html

Database name is: upload Table name is: images

id Primary int(11) AUTO_INCREMENT
name varchar(100)

size int(11)

type varchar(20)

content mediumblob

The upload works great but just cant get it to show the image of be able to use it in a html page. When I upload the image I get as a return: for number 11 image stored but I don't see an image: Here is my code, so please, please someone help as I have been at this for ages with no result.

my upload.php code:

<?php

// Check for post data.
if ($_POST && !empty($_FILES)) {
$formOk = true;

//Assign Variables
$path = $_FILES['image']['tmp_name'];
$name = $_FILES['image']['name'];
$size = $_FILES['image']['size'];
$type = $_FILES['image']['type'];

if ($_FILES['image']['error'] || !is_uploaded_file($path)) {
    $formOk = false;
    echo "Error: Error in uploading file. Please try again.";
}

//check file extension
if ($formOk && !in_array($type, array('image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif'))) {
    $formOk = false;
    echo "Error: Unsupported file extension. Supported extensions are JPG / PNG.";
}
// check for file size.
if ($formOk && filesize($path) > 500000) {
    $formOk = false;
    echo "Error: File size must be less than 500 KB.";
}

if ($formOk) {
    // read file contents
    $content = file_get_contents($path);

    //connect to mysql database
    if ($conn = mysqli_connect('localhost', 'root', '', 'upload')) {
        $content = mysqli_real_escape_string($conn, $content);
        $sql = "insert into images (name, size, type, content) values ('{$name}', '{$size}', '{$type}', '{$content}')";

        if (mysqli_query($conn, $sql)) {
            $uploadOk = true;
            $imageId = mysqli_insert_id($conn);
        } else {
            echo "Error: Could not save the data to mysql database. Please try again.";
        }

        mysqli_close($conn);
    } else {
        echo "Error: Could not connect to mysql database. Please try   again.";
        }
    }
}
?>

<html>
<head>
    <title>Upload image to mysql database.</title>
    <style type="text/css">
        img{
            margin: .2em;
            border: 1px solid #555;
            padding: .2em;
            vertical-align: top;
        }
    </style>
</head>
<body>
    <?php if (!empty($uploadOk)): ?>
        <div>
            <h3>Your Image has been Uploaded:</h3>
        </div>
        <div>
            <img src="image.php?id=<?=$imageId ?>" width="300px" height="300px"></img>
            <strong><br /><br />Embed</strong>: <input size="30" value='<img src="image.php?id=<?=$imageId ?>">'>
        </div>

        <hr>
    <?php endif; ?>

    <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data" >
      <div>
        <h3>Image Upload:</h3>
      </div>
      <div>
        <label>IMAGE SELECT<br /></label>
        <input type="hidden" name="MAX_FILE_SIZE" value="500000">
        <input type="file" name="image" />
        <br /><br />
        <input name="submit" type="submit" value="Upload Image">
      </div>
    </form>
</body>
</html>
  • 1
    Don't save files in a database. http://stackoverflow.com/a/38829952/267540 – e4c5 Sep 23 '16 at 11:54
  • Why are you putting image data into the database in the first place? That is usually better kept in the file system. And since you call image.php to show the image, don’t you think the code of _that_ script might be more relevant …? Anyway, call that script with the same parameters directly in your browser (and comment out any `header` calls in that script before), and check the result. – CBroe Sep 23 '16 at 11:55

2 Answers2

0

It seems to be a duplicate from the issue there PHP display image BLOB from MySQL

Hope it helps.

Community
  • 1
  • 1
CodingNagger
  • 1,178
  • 1
  • 11
  • 26
0

If you store the image data in database, use the next image.php:

<?php

  $id = $_GET['id'];

  $link = mysql_connect('localhost', 'root', '');
  mysql_select_db('upload');
  $sql = "SELECT content FROM images WHERE id=$id";
  $result = mysql_query("$sql");
  $row = mysql_fetch_assoc($result);
  mysql_close($link);

  header("Content-type: image/jpeg");
  echo $row['content'];
?>
T K
  • 1
  • 2