The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Image won't upload to database?!

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #242585

    I am making an admin system basically that will look like blog posts on the homepage and in the panel as you can see here it requires an image to be uploaded. In my database I have the column for the image set to the following: but when I try to upload the BLOB in the database is empty still and actually doesn’t record any column what so ever! When I remove some code that involves the image then it works again but that defeats the purpose because I need an image that uploads to database upon being submitted.

    The form / query:

    <form method="post" action="includes/uploadPost.php" enctype="multipart/form-data">
        <input class="posts-input" type="text" name="title" placeholder="headline" required><br />
        <input class="posts-input" type="date" name="date" required><br />
        <p class="title2">Select image to upload:</p>
        <input class="posts-input" type="file" name="image" id="image">
        <input class="posts-input posts-summary" type="text" name="summary" placeholder="Summary" required><br />
        <input class="posts-submit" value="Submit Post" type="submit">

    The action (uploadPost.php):

    $dbUsername = 'root';
    $dbPassword = '';
    // posts
    $connStr = 'mysql:host=localhost;dbname=heartfx_website';
    $conn = new PDO($connStr, $dbUsername, $dbPassword);
    if (!$conn) {
        die('Could not connect to database.');
    $headline = $_POST['title'];
    $date = $_POST['date'];
    $image = $_POST['image'];
    $summary = $_POST['summary'];
    $posts = $conn->prepare("INSERT INTO posts (title, date, image, summary) VALUES (:title, :date, :image, :summary)");
    $posts->bindParam(':title', $headline);
    $posts->bindParam(':date', $date);
    $posts->bindParam(':image', $_FILES['file']['type']);
    $posts->bindParam(':summary', $summary);
    // Image upload here!
    $target_dir = "/../uploads/";
    $target_file = $target_dir . basename($_FILES["image"]["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["image"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    // Check file size
    if ($_FILES["image"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" ) {
        echo "Sorry, only JPG, JPEG & PNG files are allowed.";
        $uploadOk = 0;
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
    header('Location: ../index.php');
    print('Record successfully updated in database!');

    Any help would be greatly appreciated!


    Your insert tries to use $_FILES['file']['type'] but shouldn’t it be $_FILES['image']['type'] given that image is the name of the input from your form?


    Yes thank you, last night I fixed my code and it works correctly now! Would you happen to know how I can make it so only images that are either 1920px wide and 1080px height OR 1280px wide and 720px height will be accepted? Any other images will provide an error. Not completely sure how to make an if statement that targets the width(s) and height(s) of the images. You can view the file I am talking about to get this at:

    Best regards,


    It looks like you actually removed the code that would make that possible…

    getimagesize() as per the php documentation provides some basic information about the image being uploaded, such as the width and height.

    From there it would be as simple as an if/else statement along the lines of:

    if ( ($w == 1920 AND $h == 1080) OR ($w == 1280 AND $h == 720) ) {
        // good
    } else {
        // bad

    But note that the above is VERY rigid, and requires that the file be exactly those dimensions. If you wanted to suppose something less than or equal to you’d have to adjust your operators accordingly.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.