【问题标题】:Uploading and displaying image to mysql using html and php使用 html 和 php 将图像上传并显示到 mysql
【发布时间】:2019-04-08 14:45:37
【问题描述】:

我正在尝试将我的 HTML 表单的图像上传到我的 MySQL blob 列中。插入已成功完成,但知道直接插入 MySQL 的图像显示正确,图像的显示无法正常工作。

HTML 代码:

 <form class="form-horizontal" method='POST' action="add_question_submit.php" id="addQuestionForm" enctype="multipart/form-data">
          <div class="form-group">
              <textarea rows="4" cols="50" name="question" form="addQuestionForm" placeholder="Enter Question in brief.... " required></textarea><br>
              <input type="file" class="form-control" id="image" name="image" required><br>
              <input type="text" class="form-control" id="answer" placeholder="Enter Correct Answer" name="answer"  required><br>
              <input type="number" class="form-control" id="category_id" placeholder="Enter category id (only numeric)" name="category_id" required><br>
              <input type="number" class="form-control" id="level_id" placeholder="Enter level id (only numeric)" name="level_id" required><br>
              <input type="submit" name="submit" value="submit" class="btn btn-primary">
          </div>
        </form>

PHP 代码:

$file_temp = base64_encode( file_get_contents( $_FILES['image']['tmp_name'] ) ); 
$image = getimagesize($file_temp);
$query = "INSERT INTO  questions(question_name, image, answer, category_id,level_id)VALUES('$question', '$image','$answer', '$category_id', '$level_id')";
$result = mysqli_query($conn, $query);
header("Location: display_question.php");

display_question.php:

<td><?php echo '<img src="data:image/png;base64,'.base64_encode($row['image']).'" />'; ?></br><br/></td>

【问题讨论】:

  • 在表格中存储图像是个坏主意
  • @SureshKamrushi,你有更好的主意吗?
  • 你不是存储getimagesize()的结果而不是图像吗?
  • 我已经尝试过存储变量 $file_temp 但没有任何反应。总是插入完成,显示不工作。
  • 正如@Suresh Kamrushi 所说,最好将图像存储在文件系统的目录中,并将对图像的引用存储在数据库中。

标签: php mysql image file-upload base64


【解决方案1】:

以下是将图像上传到特定文件夹的功能。 你可以像下面这样的功能:

Param 1: is the folder where we need to store the new image  
Param 2: is FILES  
Param 3: if any prefix for the image we need to pass it.  
Param 4: if there is any previously uploaded image for same record. It should be deleted from the folder. Generally it is usefull when you are editing particluar record.  

uploadfile("profile",$_FILES,"profile_pic");

代码在这里:

 function uploadfile($folder,$data,$preFix = "logo_",$previousImage="") {
        $location = (pathinfo($_SERVER['SCRIPT_FILENAME']));
        $uploads_dir = $location['dirname'] . "/assets/uploads/" . $folder . "/";
        $fileNames = "";
        $tmp_name = $data["tmp_name"];
        $name = $data["name"];
        $fileExtension = pathinfo($name, PATHINFO_EXTENSION);
        $newfilename = $preFix . date("ymdhms") . '.' . $fileExtension;
        $fulldest = $uploads_dir . $newfilename;
        if (move_uploaded_file($tmp_name, $fulldest)) {
            if($previousImage != "")
                $this->removePreviousImage($folder, $previousImage);     //deleting existing image
            return $newfilename;
        } else {
            exit("Error File Uploading");
        }
    }

【讨论】:

    【解决方案2】:

    首先,您将错误的信息存储到数据库中,您存储的是文件大小而不是编码图像。

    $file_temp = base64_encode( file_get_contents( $_FILES['image']['tmp_name'] ) ); 
    $image_size = getimagesize($file_temp);
    $query = "INSERT INTO  questions(question_name, image, answer, category_id,level_id)
                            VALUES('$question', '$file_temp','$answer', '$category_id', '$level_id')";
    $result = mysqli_query($conn, $query);
    header("Location: display_question.php");
    

    其次,如果您已将文件的 base64 编码版本存储到数据库中,则在从数据库中检索文件时无需再次对其进行编码,因此您的 &lt;img&gt; 标签应该是

    <?php echo "<img src='data:image/png;base64,$row[image]'/>"?>
    </br><br/></td>
    

    第三,也是最重要的,您需要使用参数化绑定查询来保护您的应用免受SQL Injection Attack

    $file_temp = base64_encode( file_get_contents( $_FILES['image']['tmp_name'] ) ); 
    $image_size = getimagesize($file_temp);
    $query = "INSERT INTO  questions
                          (question_name, image, answer, category_id,level_id)
                    VALUES(?,?,?,?,?)";
    
    $stmt = $conn->prepare($query);
    $stmt->bind_params('sssss',  $question, 
                                $file_temp,
                                $answer,
                                $category_id,
                                $level_id);
    $result = $stmt->execute();
    if (!$result) {
        echo $conn->error;
    }
    header("Location: display_question.php");
    

    【讨论】:

    • 感谢您的回复,但我有这个错误:“解析错误:语法错误,意外'$result' (T_VARIABLE) in ...”
    • 抱歉,遗漏了几个逗号。再试一次
    猜你喜欢
    • 2014-03-28
    • 2014-06-18
    • 1970-01-01
    • 2015-06-17
    • 2016-05-25
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多