为什么我使用 PHP 上传的图片没有正确存储在 longblob 中
Why is my image uploaded using PHP not being stored correctly in a longblob
我在 MySQL 数据库中存储 .JPG
图像时遇到问题。我正在使用下面的 PHP 代码将其存储到数据库中(以长 blob 形式)。我的代码似乎完全没有错误,除了图像仅以 36 B 大小存储在 PHPMyAdmin.
之外,一切似乎都按预期运行。
我希望图像更大(应该是 22 MB)。
<?php
require '../FirstWebsite/CommonFunctions.php';
DB_Connect();
$dblink = DB_Connect();
?>
<html>
<body>
<form method="post" enctype="multipart/form-data">
<br/>
<input type="file" name="selectImage">
<br/>
<input type="submit" name="submitImage" value="submit">
</form>
<?php
if(isset($_POST['submitImage'])){
if(getimagesize($_FILES['selectImage']['tmp_name'])==FALSE){
echo 'please select an actual image';
}
else{
$getImage = mysqli_real_escape_string($dblink, $_FILES['selectImage']['tmp_name']);
$name = mysqli_real_escape_string($dblink, $_FILES['selectImage']['name']);
$image = base64_encode($getImage);
saveImage($name, $image, $dblink);
}
}
function saveImage($name, $image, $dblink){
$query = "INSERT INTO trialtable2 (caption, actualpicture) VALUES ('$name','$image')";
$queryCheck = mysqli_query($dblink, $query);
if($queryCheck == TRUE){
echo '<br/> uploaded';
}
else{
echo '<br/> not uploaded';
}
}
?>
</body>
</html>
我做了什么
尝试上传.jpeg
.JPG
.jpg
(相同但仍然...).tif
.png
file_upload = on
(或其他)已经在 php.ini
在数据库中是long blob类型table
据说将图像存储在数据库中是行不通的,但这就是我目前正在使用的方法。
版本
- 沼泽:2.5
- 阿帕奇:2.4.9
- mysql: 5.6.17
- php: 5.5.12
我相信这个问题不是复制品,因为我在别处找不到答案。如果我做错了什么,请随时告诉我,因为我还是社区的新手。
tmp_name
是文件的临时路径,这就是您只看到几个字节的原因。
- 删除所有转义(包括
addslashes
)。
- 使用
file_get_contents($_FILES['selectImage']['tmp_name'])
获取图像的实际内容以插入到您的数据库中。
通常isn't a great idea to store the blobs in the database, and better to store the path (like you're currently doing). If you choose to go the path route, you need to use something like move_uploaded_file
将文件移动到固定位置。
我在 MySQL 数据库中存储 .JPG
图像时遇到问题。我正在使用下面的 PHP 代码将其存储到数据库中(以长 blob 形式)。我的代码似乎完全没有错误,除了图像仅以 36 B 大小存储在 PHPMyAdmin.
我希望图像更大(应该是 22 MB)。
<?php
require '../FirstWebsite/CommonFunctions.php';
DB_Connect();
$dblink = DB_Connect();
?>
<html>
<body>
<form method="post" enctype="multipart/form-data">
<br/>
<input type="file" name="selectImage">
<br/>
<input type="submit" name="submitImage" value="submit">
</form>
<?php
if(isset($_POST['submitImage'])){
if(getimagesize($_FILES['selectImage']['tmp_name'])==FALSE){
echo 'please select an actual image';
}
else{
$getImage = mysqli_real_escape_string($dblink, $_FILES['selectImage']['tmp_name']);
$name = mysqli_real_escape_string($dblink, $_FILES['selectImage']['name']);
$image = base64_encode($getImage);
saveImage($name, $image, $dblink);
}
}
function saveImage($name, $image, $dblink){
$query = "INSERT INTO trialtable2 (caption, actualpicture) VALUES ('$name','$image')";
$queryCheck = mysqli_query($dblink, $query);
if($queryCheck == TRUE){
echo '<br/> uploaded';
}
else{
echo '<br/> not uploaded';
}
}
?>
</body>
</html>
我做了什么
尝试上传
.jpeg
.JPG
.jpg
(相同但仍然...).tif
.png
file_upload = on
(或其他)已经在php.ini
在数据库中是long blob类型table
据说将图像存储在数据库中是行不通的,但这就是我目前正在使用的方法。
版本
- 沼泽:2.5
- 阿帕奇:2.4.9
- mysql: 5.6.17
- php: 5.5.12
我相信这个问题不是复制品,因为我在别处找不到答案。如果我做错了什么,请随时告诉我,因为我还是社区的新手。
tmp_name
是文件的临时路径,这就是您只看到几个字节的原因。
- 删除所有转义(包括
addslashes
)。 - 使用
file_get_contents($_FILES['selectImage']['tmp_name'])
获取图像的实际内容以插入到您的数据库中。
通常isn't a great idea to store the blobs in the database, and better to store the path (like you're currently doing). If you choose to go the path route, you need to use something like move_uploaded_file
将文件移动到固定位置。