1

I am having a strange problem where my query to Insert values into a database are not being added. I am using MySQL through terminal and the file I am doing these queries through are on my AWS ubuntu instance.

Here is the upload.php file located on my AWS instance:

<?php

    if(isset($_POST["submit"])){
        $check = getimagesize($_FILES["image"]["tmp_name"]);
        if($check !== false){
            $image = $_FILES['image']['tmp_name'];
            $imgContent = addslashes(file_get_contents($image));

            $servername = "localhost";
            $username = "NOTREAL";
            $password = "NOTREAL";
            $dbname = "CS4830";

            //connect
            $db = new mysqli($servername, $username, $password, $dbname); 

            // Check connection
            if($db->connect_error){
                die("Connection failed: " . $db->connect_error);
            }
            else{
                echo "Successful";
            }

            $dataTime = date("Y-m-d H:i:s");    

            //Insert image content into database
            $insert = $db->query("INSERT into images (image, created) VALUES ('$imgContent', '$dataTime')");
            if($insert){
                echo "<br/>";
                echo "File uploaded successfully.";
            }else{
                echo "<br/>";
                echo "File upload failed, please try again. ";
            } 
        }else{
            echo "Please select an image file to upload.";
        }
    }

?>

When I upload an image and the php runs, I end up getting "File upload failed, please try again." printed out on my screen. - The table is named images. Can Anyone identify the possible issue? I thought maybe it had something to do with permissions but the credentials are correct and I can't imagine what permissions would be lacking. Other than that I'm lost at this point. Thank you.

(A simple demo site) http://ec2-54-158-61-31.compute-1.amazonaws.com/images/

  • Hello, have you checked if the creds you're using ('$username'@'$servername') has the correct `GRANTS` to process your request? – Avidos Sep 07 '18 at 04:33
  • `print_r($imgContent)` print this line and show the result – Bilal Ahmed Sep 07 '18 at 04:36
  • @Avidos I have not checked what they are granted, how can I? My username for my apache server (cgcnbf) is different then the one MySQL (root) had me sign in with. Could that be the problem? – Christian Caldwell Sep 07 '18 at 04:37
  • @BilalAhmed I did the print and it was a super long string with symbols, characters, and question marks. I will update the link above to my server so you can see. – Christian Caldwell Sep 07 '18 at 04:42
  • That can only cause a problem if the one you are using does not have a proper `GRANT` to the ``.`` you're gonna process into.. try `SHOW GRANTS FOR ''@'';` e.g. `SHOW GRANTS FOR 'cgcnbf'@'%';`
    – Avidos Sep 07 '18 at 04:43
  • I believe that you database isn't large enough or configured to hold an image. – Forbs Sep 07 '18 at 04:44
  • oooh. you need image upload code.. – Bilal Ahmed Sep 07 '18 at 04:48
  • @Avidos it says `There is no such grant defined for user 'cgcnbf' on host 'greentree'` – Christian Caldwell Sep 07 '18 at 04:50
  • That confirms it, kindly ask your administrator to give your user proper `GRANTS` for this :) – Avidos Sep 07 '18 at 04:52
  • @Avidos I don't mean to look stupid here but who exactly is the administrator? I started the instance. – Christian Caldwell Sep 07 '18 at 04:55
  • The person who has the access to use `root` and provide what you need – Avidos Sep 07 '18 at 05:26
  • @Avidos thats me. What directory or file do I need to be in to change permissions? – Christian Caldwell Sep 07 '18 at 05:34
  • I see, considering your current parameters.. GRANT INSERT ON `CS4830`.`images` TO 'cgcnbf'@'localhost'; AND GRANT INSERT ON `CS4830`.`images` TO 'cgcnbf'@'%'; PS: You have to use the `root` cred to give this permission – Avidos Sep 07 '18 at 06:58

3 Answers3

0
$file_name = $_FILES['image']['name']; 
$tmpfilename = $_FILES["image"]["tmp_name"]; 
move_uploaded_file($tmpfilename,"images/".$file_name);  
Raushan Singh
  • 1,070
  • 1
  • 9
  • 18
0

There is multiple way to store image

if you want to store image into database then you need blob datatype (column data type must be BLOB) then you can used below code

$imagename=$_FILES["myimage"]["name"]; 

//Get the content of the image and then add slashes to it 
$imagetmp=addslashes (file_get_contents($_FILES['myimage']['tmp_name']));

//Insert the image name and image content in image_table
$insert_image="INSERT INTO image_table VALUES('$imagetmp','$imagename')";

mysql_query($insert_image);

Code reference

if you want to add image into directory and store name into database then

$uploaddir = '/var/www/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

    echo "<p>";

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
      echo "File is valid, and was successfully uploaded.\n";
    } else {
       echo "Upload failed";
    }
//and then execute insert query with image name instead of image

code reference

Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
0

You can't store the image content in this way. So there are some issue in "$imgContent = addslashes(file_get_contents($image));"

If you want to save image data into database then convert the image content into base64 encode and then try to save the encoded data.

Suman Biswas
  • 853
  • 10
  • 19