0

I've uploaded an image and text directly to my table through phpMyAdmin. However when it comes to displaying, the images are showing up as junk text. What could be the issue? The image is a relatively small jpg file. Here is the code:

<?php
    require_once 'login.php';
    $conn = new mysqli($hn, $un, $pw, $db);
    if($conn->connect_error) die($conn->connect_error);
    $query = "SELECT * from classics";
    $result = $conn->query($query);
    if(!$result) die($conn->error);

    $rows = $result->num_rows;

    for($j=0; $j < $rows; $j++) {
         $result->data_seek($j);
         $row=$result->fetch_array(MYSQLI_ASSOC);
         echo 'Cover:' .$row['sleeve'] .'<br>'; 
    }

    $result->close();
    $conn->close();
?>
5
  • what junk text is that? can you add it? Commented Jan 5, 2016 at 10:10
  • along these lines: Cover:����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ��C $.' ",#(7),01444'9=82<.342��C 2!!22222222222222222222222222222222222222222222222222��~,"����H!1AQ"aq2���#5BRs���3r$b�����%46t�'CTd�����6 !1A"Qaq23������4B��#$�Rb��?;J�*�Om*U��'�RI�G�,�Z�lc_p�;&%�� ������>3�~������L�#֕k����*�� t ����մg1�/��,ٷ|�Y�q�� Commented Jan 5, 2016 at 10:14
  • @R34nimated: pls explain? does that mean I uploaded the same image more than once? Commented Jan 5, 2016 at 10:29
  • @AgniScribe Try replacing your echo statement with the code in my answer below. Commented Jan 5, 2016 at 10:59
  • 1
    You cannot just output an image as raw data, you have to place it in an <img> tag Commented Jan 5, 2016 at 11:00

2 Answers 2

2

Something like this as your echo statement "should" work with your current implementation.

//echo 'Cover:' .$row['sleeve'] .'<br>'; 
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['sleeve'] ).'"/>';

However @Noor is correct, storing images like this is not very efficient.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, that works; though will take on board the right way of uploading mentioned
@AgniScribe Good deal, will you mark as correct?
2

You are doing it the wrong way. Don't save images in database as it is not a good practice. Just save the uploaded image in a directory and save the path to that image in database. It would save you some database space and search time.

Here is some sample code

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

Refer to http://www.w3schools.com/php/php_file_upload.asp for help

5 Comments

@ noor: but given that the image is now already in the database, why should it display as junk; my problem is with the display.
stackoverflow.com/questions/20556773/… because it isn't encoded correctly.
thanks, ok, so then if I uploaded it the right way, will it display the right way with the original code i used? am also trying it out...
@AgniScribe yes ... you have to just place an <img> tag and in src attribute of <img> tag you have to place the path you stored in database. thats it
@AgniScribe study this article too before implementing it .htmlgoodies.com/beyond/php/article.php/3877766/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.