You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP展示餐厅图片出现裂图问题求助(路径存Blob字段)

Fixing Broken Restaurant Logo Images in PHP

Hey there, let's figure out why your restaurant logos are showing up as broken images and get this fixed for you. Looking at your code and setup, there are a few key issues we need to address:

1. Wrong Database Field Type for Logo Path

You mentioned storing the file path in the Logo field, but you're using a BLOB type for it. BLOB is designed to store binary data (like the actual image file bytes), not text strings like file paths. This can cause the path to be stored incorrectly or retrieved as garbled data.

Fix:
Alter your restaurant table to change the Logo field to a VARCHAR type (e.g., VARCHAR(255) is usually enough for file paths). Run this SQL query in your database:

ALTER TABLE restaurant MODIFY COLUMN Logo VARCHAR(255);

2. Missing File Upload Handling

Your add-restaurant code defines the $photo path, but it never actually moves the uploaded image from the server's temporary folder to your restaurantlogo/ directory. That means the file doesn't exist where your img tag is trying to load it from.

Fix:
Add code to move the uploaded file after validating the extension. Also, handle duplicate filenames (e.g., append a timestamp to avoid overwriting existing images):

// Add this after validating the extension
if(in_array($extension,$allowedExts)){
    // Generate a unique filename to avoid overwrites
    $uniqueFilename = uniqid() . '.' . $extension;
    $photo = "restaurantlogo/" . $uniqueFilename;
    
    // Move the uploaded file from temp directory to your target folder
    if(move_uploaded_file($_FILES["restaurant_logo"]["tmp_name"], $photo)){
        // File uploaded successfully, proceed with database insert
    } else {
        die("Failed to upload the image file.");
    }
}

Note: Make sure the restaurantlogo/ directory exists and has write permissions (chmod 755 or similar) so the server can save files there.

3. Undefined $filename Variable

In your add code, you use $filename here but never define it:

$extension = pathinfo($filename, PATHINFO_EXTENSION);

Fix:
Replace $filename with $_FILES["restaurant_logo"]["name"] to get the original filename from the upload:

$filename = $_FILES["restaurant_logo"]["name"];
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$extension = strtolower($extension);

4. SQL Injection Risk & Incorrect Success Check

Your insert query uses raw variable concatenation, which is a huge security risk (SQL injection). Also, checking if ($sql) is wrong—$sql is just a string, so this condition will always be true, even if the insert fails.

Fix:
Use prepared statements for the database insert, and check the result of $insert:

// Use prepared statement to prevent SQL injection
$stmt = $conn->prepare("INSERT INTO restaurant (Logo, Name, Password, ContactNumber, Address, CuisineType, SupportArea) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssss", $photo, $name, $password, $contactnum, $address, $cuisinetype, $chk);

if($stmt->execute()){
    header("Location: ../restaurant-add.php");
    exit; // Always exit after redirect
} else {
    die("Error inserting data: " . $stmt->error);
}

Note: Make sure $password is defined in your add code (I don't see it set from $_POST in your original snippet—don't forget to handle that, and always hash passwords before storing them!)

5. Clean Up the Image Display Code

Your display code has messy HTML/PHP mixing, which can lead to syntax errors. Let's clean it up and ensure the path is correct:

<?php 
$sql = "SELECT * FROM restaurant"; 
$result = $conn->query($sql) or die($conn->error); 
?>
<table>
    <?php while ($row = $result->fetch_assoc()): ?>
    <tr>
        <td><?= $row['ID'] ?></td>
        <td><img src="<?= htmlspecialchars($row['Logo']) ?>" alt="<?= htmlspecialchars($row['Name']) ?> Logo" /></td>
        <td><?= $row['Name'] ?></td>
        <td><?= $row['ContactNumber'] ?></td>
        <td><?= $row['Address'] ?></td>
        <td><?= $row['CuisineType'] ?></td>
        <td><?= $row['SupportArea'] ?></td>
    </tr>
    <?php endwhile; ?>
</table>

Using <?= ?> shorthand makes the code cleaner, and htmlspecialchars() prevents XSS vulnerabilities by escaping special characters in the data.


After making all these changes, your images should load correctly. Let me know if you run into any other issues!

内容的提问来源于stack exchange,提问作者Dlayz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:26:40