簡體   English   中英

使用 html 和 php 上傳和顯示圖像到 mysql

[英]Uploading and displaying image to mysql using html and php

我正在嘗試將我的 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>

在此處輸入圖片說明

首先,您將錯誤的信息存儲到數據庫中,您存儲的是文件大小而不是編碼圖像。

$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 編碼版本存儲到數據庫中,則在從數據庫中檢索它時不需要再次對其進行編碼,因此您的<img>標簽應該是

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

第三,也是最重要的,您需要使用參數化綁定查詢來保護您的應用程序免受SQL 注入攻擊

$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");

下面是將圖像上傳到特定文件夾的功能。 您可以使用以下所有功能:

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");
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM