亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在數據庫中插入圖像并能夠再次顯示?

如何在數據庫中插入圖像并能夠再次顯示?

PHP
忽然笑 2022-01-02 17:30:27
我正在嘗試在數據庫中插入一個圖像,然后再次顯示它。目前它不起作用。所以我的問題是如何插入圖像,然后將其顯示在頁面上。嘗試插入和選擇但它不起作用
查看完整描述

2 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

如果您想使用 php 上傳和顯示文件,請嘗試:


1) 首先,您將文件上傳到一個文件夾。本教程可能會幫助您使用 PHP 上傳文件。


2)在您的數據庫上創建一個名為“uploaded_files”的表,并在表下創建像“ id , file_url ”這樣的字段。


3)在上傳過程中將$target_file值保存到file_url字段并從數據庫中獲取。


但是,如果您想使用 blob 將文件保存在數據庫中,本教程可能會對您有所幫助。


在 php 上試試:


CREATE TABLE `uploaded_files` (

 `id` int(11) NOT NULL AUTO_INCREMENT,

 `file_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,

 PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

連接名為“db_conenct.php”的數據庫:


<?php

// Database configuration

$dbHost     = "localhost";

$dbUsername = "user";

$dbPassword = "pass";

$dbName     = "your_database_name";


// Create database connection

$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);


// Check connection

if ($db->connect_error) {

    die("Connection failed: " . $db->connect_error);

}

?>

使用 HTML 創建上傳表單,您將使用“POST”方法將數據發送到 upload.php:


<form action="upload.php" method="post" enctype="multipart/form-data">

    Select Image File to Upload:

    <input type="file" name="file">

    <input type="submit" name="submit" value="Upload">

</form>

上傳.php


<?php

// Include the database configuration file

include 'db_connect.php';

$statusMsg = '';


// File upload path

$targetDir = "uploads/";

$fileName = basename($_FILES["file"]["name"]);

$targetFilePath = $targetDir . $fileName;

$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);


if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){

    // Allow certain file formats

    $allowTypes = array('jpg','png','jpeg','gif','pdf');

    if(in_array($fileType, $allowTypes)){

        // Upload file to server

        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){

            // Insert image file name into database

            $insert = $db->query("INSERT into uploaded_files (file_url) VALUES ('".$fileName."'");

            if($insert){

                $statusMsg = "The file ".$fileName. " has been uploaded successfully.";

            }else{

                $statusMsg = "File upload failed, please try again.";

            } 

        }else{

            $statusMsg = "Sorry, there was an error uploading your file.";

        }

    }else{

        $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';

    }

}else{

    $statusMsg = 'Please select a file to upload.';

}


// Display status message

echo $statusMsg;

?>

顯示上傳的圖片:


<?php

// Include the database configuration file

include 'db_connect.php';


// Get images from the database

$query = $db->query("SELECT * FROM uploaded_files ORDER BY id DESC");


if($query->num_rows > 0){

    while($row = $query->fetch_assoc()){

        $imageURL = 'uploads/'.$row["file_url"];

?>

    <img src="<?php echo $imageURL; ?>" alt="" />

<?php }

}else{ ?>

    <p>No image(s) found...</p>

<?php } ?>


查看完整回答
反對 回復 2022-01-02
?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

我不知道我是否理解你的問題,但無論如何我試著回答它。


在我看來,有兩種方法可以做到:


將您的圖像上傳到任何存儲圖像的云或您的項目結構,并僅保存在數據庫中的圖像路徑。


獲取文件內容,然后將其保存到您的數據庫中,例如。


$file = fopen($MY_FILE, 'r');

$file_contents = fread($file, filesize($MY_FILE));

fclose($file);


/* escape some stcharacters in file_contents before query.*/

$file_contents = addslashes($file_contents);


// Add the file in the database

mysql_connect('localhost', 'root', '') or die("Unable to connect to database.");

mysql_select_db('test') or die("Unable to select the DB.");

mysql_query("INSERT INTO files SET file_data='$file_contents'") or die("MySQL Query Error: " . mysql_error() . "


". "The SQL was: $SQL


");

mysql_close();

echo "File INSERTED into files table successfully.";

要將文件二進制內容存儲在數據庫中,列必須是以下類型之一:


小斑點

BLOB

中等斑點

長斑


查看完整回答
反對 回復 2022-01-02
  • 2 回答
  • 0 關注
  • 153 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號