-1

Edit: My Ajax to send the image data is this

  var Image = $('#PostSection').find('input[name="Image2"]').val();

    var fd = new FormData();
    var files = $('#Image2')[0].files[0];
    console.log(files);
    fd.append('Image',files);
    fd.append('username',username);
    fd.append('posts',posts);
    fd.append('backText3',backText3);
    fd.append('poll',poll);
    fd.append('option1',option1);
    fd.append('option2',option2);
    fd.append('option3',option3);
    fd.append('option4',option4);
    $.ajax({
        url: '../Admin/Posts/Post.php',
        type: 'post',
        data: fd,
        contentType: false,
        processData: false,
        success:  function() {
     $('#AllPosts').prepend($('<div>').load(location.href + " 
#AllPosts>*"));
  $('#PostSection')[0].reset();
     var x = document.getElementById("snackbar2");
  x.className = "snackbar show";
  setTimeout(function(){ x.className = x.className.replace("show", ""); }, 
3000);

keep in mind I'm only showing the variables for the image since that relates to my question. I don't need to be told about the code for username,posts,poll etc. It's irrelevant to my question.

My image is retrieved like this

  $tmpName  = $_FILES['Image']['tmp_name'];
  $fp = fopen($tmpName, 'r');
  $Image = fread($fp, filesize($tmpName));
  $Image = addslashes($Image);
  fclose($fp); 

and my insertion code is like this

$stmt = $conn3->prepare("INSERT INTO `$Username` (Username,RepostedBy,RepostedId,Texts,Img,backText,Dates,TimeUploaded,EditedVersion,Likes,Repost,Tags,Poll)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");

$stmt->bind_param("ssssbssssssss",$Username,$RepostedBy,$RepostedId,$Texts,$Image,$BackText,$Date,$Time,$EditedVersion,$Likes,$Shares,$Tags,$Poll);

For some reason though, this does not work. My column for my images is longblob. Everything else is inserted, but the image isn't inserted correctly for some reason. A file gets inserted, but it isn't the image. Please note before commenting, as I stated, everything is inserted as it should except that the image isn't inserted properly. I don't need any replies asking me about my connection to my database or if any of my variables are null. Thanks!

The files being inserted are images, and my database says for some of them 129 kib when the image shouldn't be that high in storage. I tried the unprofessional method of doing this

$sql = "INSERT INTO `$Username` (Username,RepostedBy,RepostedId,Texts,Img,backText,Dates,TimeUploaded,EditedVersion,Likes,Repost,Tags,Poll) 
VALUES ('$Username','$RepostedBy','$RepostedId','$Texts','$Image','$BackText','$Date','$Time','$EditedVersion','$Likes','$Shares','$Tags','$Poll');";

and this works but it isn't a prepared statement so it isn't ideal at all or professional because I don't want to have SQL injections.

2 Answers2

0

Make sure the contents are read completely. fread buffer size should be exact for the complete image data to be valid. Try with file_get_contents once.

$Image = addslashes(file_get_contents($_FILES['Image']['tmp_name']));
Kris
  • 8,680
  • 4
  • 39
  • 67
0

It is NOT recommended to store images in the database, slow retrieval, slow everything.

What people do is, they store the image in a CDN, AWS S3 or whatever provider you prefer, and then in the database you store the URL.

Really not recommended to do what you are trying to do.

Borjante
  • 9,767
  • 6
  • 35
  • 61
  • Thanks, for the suggestion and I think I'll be going this route. Is there a way to do this easily so that my file system doesn't get cluttered. I noticed this https://stackoverflow.com/questions/2648664/image-upload-storage-strategies would this be best? – AustinVD1998 Jul 09 '19 at 18:30