简体   繁体   中英

Insert image file to MySQL database

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.

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>
...

And here is the insert code:

<?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());

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!

使用文件系统可能会更好,但是如果您坚持要按照自己的方式进行操作,因为代码似乎不是以某种形式存在,那么没有方法或提交按钮吗?

I thing that the problem is in MySql Server Networking. Check the max_allowed_packet in your configuration :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM