i used this code to upload images and display from database but not working correctly...

images.php

<?php 
    include('header.php');
?>

<center>
    <form action="addimages.php" method="post" enctype="multipart/form-data">
        <table border=0>
             <tr><td>Select Image: </td><td><input type="file" name="image"></td></tr>
             <tr><td>Caption: </td><td><input name="caption" type="text"></td></tr>
             <tr> <td colspan=2><input type="submit" name="Submit" value="Upload" ></td></tr>
         </table>
     </form>
 </center>

 Photo Gallary:
<br />
<br />
<?php
    $result = mysql_query("SELECT * FROM images ORDER BY imgid");
    while($row = mysql_fetch_array($result))
    {

    echo "<div class='img'>";
    echo "<a target='_blank' href=".$row['location'].">";
    echo "<img src=".$row['location']." width='110' height='120'>";
    echo "</a>";
    echo "<div class='desc'>".$row['caption'];
    echo "<br/>";
    echo "<a href=\"javascript:delimg('$row->imgid','$row->caption');\">Delete image</a></td>";
    echo "<form method ='POST' action='dimg.php'><input type='submit' value='delete'name='delete'></form>";
    echo "</div>";
    echo "</div>";

    }
    echo "</table>";
?>

<?php
    include('footer.php');
?>

addimages.php

        <?php
        require('../includes/config.php');
        if (!isset($_FILES['image']['tmp_name'])) {
            echo "";
            }else{
            $file=$_FILES['image']['tmp_name'];
            $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
            $image_name= addslashes($_FILES['image']['name']);

                    move_uploaded_file($_FILES["image"]["tmp_name"],"../images/" . $_FILES["image"]["name"]);

                    $location="../images/" . $_FILES["image"]["name"];
                    $caption=$_POST['caption'];

                    $save=mysql_query("INSERT INTO images (caption, location) VALUES ('$caption','$location')");
                    header("location: gallarymanager.php");
                    exit();                 
            }
        ?>

some images are displayed and some blank image fields....

Dani AI

Generated

Brief diagnosis and fix summary: blank thumbnails normally mean the DB row has an empty or incorrect file path, or the gallery page is building the <img> src incorrectly (wrong relative/absolute path). was right to check the stored path and to block empty uploads, and ’s note that fixing the scripts solved it points to an upload/insert/display mismatch rather than a browser bug.

Checklist to make uploads robust and avoid blank entries:

  • Reject empty submissions and check the upload error code (use UPLOADERR*).
  • Verify the file was actually uploaded with is_uploaded_file() and use move_uploaded_file() to place it into your uploads directory (don’t assume a copy worked).
  • Store a consistent value in the DB (prefer storing a safe filename or relative path, not the full ../ path from the script that handled the upload), and build the public URL from that when rendering. See the PHP upload docs and move_uploaded_file notes. (php.net)

Safe minimal workflow (example pattern):

  • Validate upload error and type (see exif_imagetype / Fileinfo), generate a random filename, move the file, insert the filename into DB, and use prepared statements to avoid SQL injection. Example outline:
// validate
if (!isset($_FILES['image']) || $_FILES['image']['error'] !== UPLOAD_ERR_OK) { exit; }
if (exif_imagetype($_FILES['image']['tmp_name']) === false) { exit; } // not a valid image

// store
$filename = bin2hex(random_bytes(8)) . image_type_to_extension(exif_imagetype($_FILES['image']['tmp_name']));
$dest = __DIR__ . '/uploads/' . $filename;
if (!move_uploaded_file($_FILES['image']['tmp_name'], $dest)) { exit; }

// insert with PDO
$stmt = $pdo->prepare('INSERT INTO images (caption, filename) VALUES (?, ?)');
$stmt->execute([$caption, $filename]);

Validate file contents and follow security guidance: never trust the client MIME or extension, whitelist image types, limit file size, remove execute permissions from upload folders, and consider serving uploads through a script rather than directly if possible. OWASP’s file-upload guidance lists common pitfalls and mitigations. (php.net)

About the DB API: the old mysql_* calls are deprecated/removed in modern PHP — migrate to mysqli or PDO and use prepared statements for INSERT/SELECT to avoid injection and future breakage. Also verify the upload directory permissions and that your display page uses the same URL base you stored. (wiki.php.net)

Quick troubleshooting checklist: confirm $_FILES['image']['error'] is UPLOAD_ERR_OK, check move_uploaded_file() return and server permissions, inspect the stored path in the DB (use absolute URL building or store only filename), and wrap src attributes in quotes and escape them with htmlspecialchars() when rendering.

Recommended Answers

All 3 Replies

I've tried the code and its working perfectly, check the Extensions for the files you uploaded but I don't think this affect the proccess.
you could try to see the path of blank image and you'll know what is wrong.
maybe you clicked on upload without choosing a picture that will put empty record on database, you should put a condition to check if the form is empty

if (!isset($_FILES['image']['tmp_name']) || $_FILES['image']['tmp_name'] == "" ) {
    echo "Choose a picture first";
}

thank you for the reply...i studied my scripts as you said...now it's working.

you can mark this question as solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.