gpt4 book ai didi

php - 如何使用phpmyadmin在数据库中插入图像?

转载 作者:搜寻专家 更新时间:2023-10-30 23:05:21 45 4
gpt4 key购买 nike

我使用 xampp localhost 开发网站。我想使用 phpmyadmin 添加图片,但我无法添加它。图片可以在 phpmyadmin 中显示,但不能出现在我的网站上。我的 php 编码有问题吗?

$host="localhost"; // Host name 
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="MMDB"; // Database name
$tbl_name="artist"; // Table name

// Create connection
$conn = mysqli_connect($host, $username, $password, $db_name);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$Name=$_POST['Name'];
$Hometown=$_POST['Hometown'];
$Income=$_POST['Income'];
$Image=$_POST['Image'];
//file =$_FILES['image']['tmp_name'];
$Video=$_POST['Video'];
$sql = "INSERT INTO $tbl_name(Name, Hometown, Income, Image, Video)
VALUES ('$Name', '$Hometown','$Income','$Image','$Video')";


if (mysqli_query($conn, $sql)) {

include 'admin.php';
} else {
echo "x";
}


mysqli_close($conn);
?>


在文件夹中添加图片的代码是什么?

//我应该在哪里插入这段代码 "> $image = $_FILES["图像"]["名称"]; $uploadedfile = $_FILES['image']['tmp_name']; 如果($图片){ $filename = stripslashes($_FILES['image']['name']);

         /* Ensure the file is JPG/PNG */
if (($_FILES['image']['type'] != "image/jpg") && ($_FILES['image']['type'] != "image/jpeg") && ($_FILES['image']['type'] != "image/png")) { $error_status = 2; $error_message = "File uploaded was not a JPG or PNG file."; }
else {
/* Ensure the file is less than 10MB */
$size = filesize($_FILES['image']['tmp_name']);
if ($size > (MAX_SIZE * 1024)) { $error_status = 3; $error_message = "File uploaded needs to be less than 10MB."; }
else {
$extension = "jpg";
if(($_FILES['image']['type'] == "image/jpg") || ($_FILES['image']['type'] == "image/jpeg")) { $uploadedfile = $_FILES['image']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); }
else if (($_FILES['image']['type'] == "image/png")) { $extension = "png"; $uploadedfile = $_FILES['image']['tmp_name']; $src = imagecreatefrompng($uploadedfile); }
$img_mime_type = $_FILES['image']['type'];

最佳答案

您不应该在表格中保存图像。

您应该创建一个文件夹来保存上传的图片。在上传过程中,您将图片复制到文件夹中(使用哈希来获取唯一的文件名),然后将文件名存储在表中。

当你想使用图片时,只需打开文件路径中的图片即可。

关于php - 如何使用phpmyadmin在数据库中插入图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27329607/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com