将图像文件插入MySQL数据库 [英] Insert image file to MySQL database

查看:137
本文介绍了将图像文件插入MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个在phpMyAdmin上使用MySql数据库的音乐家建立一个社交网络类型的网站。我的网站插入图像文件到数据库,然后显示它们(对于个人资料图片)有问题。图像作为LONGBLOB存储在数据库中。

I'm working on a social-network type website for musicians that uses a MySql database on phpMyAdmin. I'm having a problem with the website inserting image files to the database, and then displaying them (for profile pictures). The images are stored as LONGBLOB in the database.

如果我直接通过phpMyAdmin添加图片,它可以正常工作。根据数据库,它是以LONGBLOB数据类型存储为.bin的.jpg。在我的网站上,我可以使用以下方式查询并完美显示:

If I add a picture directly through phpMyAdmin, it works fine. It's a .jpg stored as a .bin in the LONGBLOB datatype according to the database. On my website I can query and display it perfectly using:

<img src="data:image/jpeg;base64,<?php echo base64_encode($memberpicture);?>" 
ALT="Member does not have picture" WIDTH=100 HEIGHT=100></img>

但是,如果网站用户使用网站创建个人资料并将图像作为个人资料图片插入创建配置文件功能,它不会显示在他们的个人资料上。以下是选择的图像代码:

However, if a website user creates a profile and inserts an image as a profile picture using the website's "create profile" functionality, it isn't displayed on their profile. Here is the select image code:

...
<div class="control-group">
    <div class="controls">
        <label>Band Picture</label>
        <input type="file" name="picture"/>
    </div>
</div>
...

这里是插入代码:

<?php
    session_start();
    foreach($_POST AS $key => $val) {
    $_SESSION[$key]=$val;
}

mysql_connect("***", "***", "***");
mysql_select_db("musicians");

//...declare other values(name, bio, etc...
$cpicture=$_POST['picture'];
$cpicture = stripslashes($cpicture);
$cpicture = mysql_real_escape_string($cpicture);

$profileInsert="INSERT INTO profile(PROFILE_BANDNAME,PROFILE_GENRE,PROFILE_BANDBIO,
    PROFILE_PICTURE,PROFILE_ART,PROFILE_MUSICVIDEO,PROFILE_PLAYLIST)
    VALUES ('$cbandname','$cgenre','$cbio','$cpicture','$cart','$cvideo','$cplaylist')"
or die(mysql_error());

如果我查看数据库,插入后会有一个文件,但是它非常小(大约16个字节),无法在网站上或直接从数据库中查看。根据数据库,它是以LONGBLOB数据类型存储为.bin的.bin,而不是存储的.jpg作为一个.bin。
对此插入的任何帮助都将非常感激!

If i look at the database, there is a file there after the insert, but it is extremely small (approx 16 bytes), and it cannot be viewed on either the website or directly from the database. It's a .bin stored as a .bin in the LONGBLOB datatype according to the database, as opposed to a .jpg stored as a .bin. Any help with this insert would be much appreciated!

推荐答案

不确定这是否是一个差异,b当我在谷歌上搜索时,你的保存代码看起来与前几个结果不同:

Not sure if this makes a difference, but your code for saving looks different from all of the first few results when i searched on google:

http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading- files-to-mysql-database.aspx
http://mirificampress.com/permalink/ saving_a_file_into_mysql
http://bytes.com/topic/php/insights/740327-uploading-files-into-mysql-database-using-php

也许只是尝试代码交换。

maybe just try a code swap.

我会回应其他关于使用文件系统的评论/答案。但是,如果你只是试图让它像你在一些回复中提到的那样工作,只需从另一个网站复制一些工作代码。

I would echo the other comments/answers here regarding just using the file system. But if you are just trying to get it to work as you mentioned in some of your replies, just copy some working code from another site.

这篇关于将图像文件插入MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆