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/

13
  • Hello, have you checked if the creds you're using ('$username'@'$servername') has the correct GRANTS to process your request? Commented Sep 7, 2018 at 4:33
  • print_r($imgContent) print this line and show the result Commented Sep 7, 2018 at 4: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? Commented Sep 7, 2018 at 4: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. Commented Sep 7, 2018 at 4:42
  • That can only cause a problem if the one you are using does not have a proper GRANT to the <database>.<table> you're gonna process into.. try SHOW GRANTS FOR '<your_user>'@'<target_host>'; e.g. SHOW GRANTS FOR 'cgcnbf'@'%'; Commented Sep 7, 2018 at 4:43

3 Answers 3

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

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

Comments

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.

Comments

0
$file_name = $_FILES['image']['name']; 
$tmpfilename = $_FILES["image"]["tmp_name"]; 
move_uploaded_file($tmpfilename,"images/".$file_name);  

3 Comments

also mention the flow of insert query
use $conn = mysqli_connect($server,$user,$psw,$db);
welcome to stackoverflow! your answer is good but incomplete and not well formatted. this will impact on your reputation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.