0

I'm pretty sure my code is correct. The issue I have is that, when viewing the image, there is only some of the image that is showing. I think it has something to do with my database that i'm uploading to, but I can't figure out what

Here is my code:

form for the upload

    <form id="form2" method="post" name="postForm" action="post.php" enctype="multipart/form-data">
<h2>Post News here</h2>
<p>Title:</p>
<input type="text" required placeholder="Enter a title" maxlength="30" name="postTitle">
<p>Body:</p>
<input type="textarea" required placeholder="Enter a body" maxlength="250" name="postBody">
<p>Link/ Reference:</p>
<input type="text" required placeholder="Enter a link or reference" maxlength="100" name="postLinkRef">
<p>Image to upload:</p>
<input type="file" name="imageTest">
<p><input type="submit" value="Submit" name="submit"></p>
</form>

inserting the image

<?php
$host = "localhost";
$userName = "root";
$password = "password";
$db = "userdata";

$connect = mysqli_connect($host,$userName,$password, $db);


if($connect)
{
    if(isset($_POST['submit']) && isset($_FILES['imageTest']))
        {
        $postTitle = mysqli_real_escape_string($connect,$_POST['postTitle']);
        $postBody = mysqli_real_escape_string($connect,$_POST['postBody']);
        $postLink = mysqli_real_escape_string($connect,$_POST['postLinkRef']);
        //address, postcode
        //admin priveleges for scertain accounts
        //wont allow me to use $_FILES and i dont know why...
        $postImageName = mysqli_real_escape_string($connect, $_FILES['imageTest']['name']); 
        $postImageData = mysqli_real_escape_string($connect, file_get_contents($_FILES["imageTest"]["tmp_name"]));
        $postImageType = mysqli_real_escape_string($connect, $_FILES["imageTest"]["type"]);
        $sql = "INSERT INTO news(title, body, link, imageName, imageData) VALUES('$postTitle','$postBody','$postLink','$postImageName','$postImageData')";
        if(mysqli_query($connect, $sql))
        {
            echo "Your post has been uploaded\n\n";
            echo "Thank you for your post $testUserName\n\n";
            echo "<a href='index.php'>Got to your news </a>";
        }
        else
        {
            echo "Sorry, something went wrong!! Check to make sure the image your are uploading is a jpeg image (not any other file)!";
        }
        }
        mysqli_close($connect);
}
else
{
    echo "Fail connection";
}
?>

Showing the image

<?php

$host = "localhost";
$userName = "root";
$password = "password";
$db = "userdata";

$connect = mysqli_connect($host,$userName,$password, $db);

    if(isset($_GET['postID']))
    {
        $id = mysqli_real_escape_string($connect, $_GET['postID']);
        $q = "SELECT * FROM news WHERE postID = $id";
        $r = mysqli_query($connect, $q);
        if(mysqli_num_rows($r)==1)//id found in the table
        {
            $row = mysqli_fetch_assoc($r);
            $imageData = $row["imageData"];
        }
        header("Content-type:image/jpeg");
        echo $imageData;
    }
    else
    {
        echo "error";
    }
?>

[enter image description here1[enter image description here2enter image description here

Josh Hughes
  • 33
  • 1
  • 4
  • You're inserting raw binary content to the database (via `$postImagedata`). Is that a binary column? – timclutton Mar 15 '16 at 12:22
  • the column is a blob type in my database? If i try and edit the attributes for that type and click binary it says i have an error with my syntax "binary not null" is this because the lines above may not necessarily have an image? – Josh Hughes Mar 15 '16 at 12:27
  • 1
    instead of saving binary content to database why don't you simply store image physically and save its path to the database, doing this your queries will work faster... – Sagar Guhe Mar 15 '16 at 12:31
  • A BLOB (Binary Large OBject) column should be fine for this. Can you add more detail to your question about the actual problem you are seeing? Screenshots would be very helpful. – timclutton Mar 15 '16 at 12:36
  • I've included the screenshots now – Josh Hughes Mar 15 '16 at 13:29
  • 1
    Those images look quite large. According to [this answer](http://stackoverflow.com/a/5775601/3775731) the limit of a `BLOB` column is about 64KiB. You probably need to change the type to a `LONGBLOB` as per that answer. – timclutton Mar 15 '16 at 13:41
  • That worked thanks a lot for the answer, I didn't realise there was a longblob available – Josh Hughes Mar 15 '16 at 13:46

0 Answers0