1

I'm just creating a simple image upload to MySQL

<form method="post" action="quanly.php" class="form-group justify-content-center" enctype="multipart/form-data">
               <div class="custom-file mb-3">
                 <input type="file" class="custom-file-input" id="image" name="image">
                 <label class="custom-file-label" for="customFile">Choose file</label>
               </div>
               <div class="col text-center">
                  <button type="submit" name="submit" id="add_btn" class="btn btn-primary mb-2"> <i class="fas fa-plus"></i>Submit</button>
               </div>
            </form>

And my php code:

$image = addslashes(file_get_contents($_FILES['image']['name']));
   $query = "INSERT INTO tasks (image) VALUES ('$image')";
   mysqli_query($db, $query); //db is the mysql connection

The image in MySQl is LONGBLOB. THE PROBLEM: when I try to submit an image, no data was in the database

2 Answers 2

1

Remove file_get_contents from image tags.

$image = addslashes($_FILES['image']['name']);
   $query = "INSERT INTO tasks (image) VALUES ('$image')";
   mysqli_query($db, $query); //db is the mysql connection
Sign up to request clarification or add additional context in comments.

Comments

1
<?php
$target_dir = "quanly/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(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;
    }
}
?>

 $target_dir = "quanly/" - specifies the directory where the file is going to be placed.
 $target_file specifies the path of the file to be uploaded
 $uploadOk=1 is not used yet (will be used later)
 $imageFileType holds the file extension of the file (in lower case)

Next, check if the image file is an actual image or a fake image

Try this..

Comments

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.