使用 PHP 向 MySql 服务器上传和检索图像的示例代码

发布于 2024-12-11 22:41:12 字数 120 浏览 0 评论 0原文

如何使用 PHP 在 MySql 数据库中上传和检索图像。我需要示例代码将图像转换为等效的二进制图像,反之亦然,以便可以以 BLOB 格式在 MySql 数据库中存储和检索它们。我在网上尽力了,但还是不行。任何帮助将不胜感激。

How to upload and retrieve images to and from MySql database using PHP. I need sample code to convert images into binary equivalent and vice versa so that they can be stored and retrieved to and from MySql database in the BLOB format. I tried my best online but I couldn't. Any help will highly be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心↖ 2024-12-18 22:41:12

检查此链接,它们可能会帮助您开始

在 mysql 中存储图像

如何用php将图片存储到mysql数据库

Check this links they may help u to get started

storing images in mysql

and

How to Store images in mysql database with php

腻橙味 2024-12-18 22:41:12
CREATE TABLE `pix` 
  ( 
     `pic_id`   INT(11) NOT NULL auto_increment, 
     `pic_name` VARCHAR(100) NOT NULL, 
     `pic_data` LONGBLOB NOT NULL, 
     PRIMARY KEY (`pic_id`) 
  ) 
engine=innodb 
DEFAULT charset=latin1; 

获取 2 个 php 文件:- Image.php、showImage.php。

相应地设置连接参数。

/* Image.php*/

<?php

    $con = mysql_connect("127.0.0.1:3306", "root", "");

    if (!$con) {
        die("Could not connect: " . mysql_error());
    }

    $DB = mysql_select_db("test", $con);
    move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "latest.img");
    $instr = fopen("latest.img","rb");
    $image = addslashes(fread(fopen("latest.img","r"),filesize("latest.img")));
    mysql_query ("insert into pix (pic_name, pic_data) values ("myImage", "'.$image.'");");

?>
<html>
    <form enctype="multipart/form-data" method="POST">

        <img src=showImage.php?gim=1 width=500 height=150 alt="hell">

        <br>
        <hr>

        <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload(
        <100 KB): <input name="uploadedfile" type="file" />
        <br/>

        <input type="submit" value="submit" name="submit" />    
    </form>
<html>
/*showImage.php*/

<?php

    $con = mysql_connect("127.0.0.1:3306", "root", "");

    if (!$con) {
        die(“Could not connect: ” . mysql_error());
    }

    $DB = mysql_select_db("test", $con);
    $res = @mysql_query("select * from pix order by pic_id desc limit 1");

    if ($row = @mysql_fetch_assoc($res)) {

        $title = htmlspecialchars($row[pic_name]);
        $bytes = $row[pic_data];

    }

    header("Content-type: image/jpg");
    print $bytes;
    mysql_close();
?>
CREATE TABLE `pix` 
  ( 
     `pic_id`   INT(11) NOT NULL auto_increment, 
     `pic_name` VARCHAR(100) NOT NULL, 
     `pic_data` LONGBLOB NOT NULL, 
     PRIMARY KEY (`pic_id`) 
  ) 
engine=innodb 
DEFAULT charset=latin1; 

Get 2 php files:- Image.php, showImage.php.

Set Connection parameters accordingly.

/* Image.php*/

<?php

    $con = mysql_connect("127.0.0.1:3306", "root", "");

    if (!$con) {
        die("Could not connect: " . mysql_error());
    }

    $DB = mysql_select_db("test", $con);
    move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "latest.img");
    $instr = fopen("latest.img","rb");
    $image = addslashes(fread(fopen("latest.img","r"),filesize("latest.img")));
    mysql_query ("insert into pix (pic_name, pic_data) values ("myImage", "'.$image.'");");

?>
<html>
    <form enctype="multipart/form-data" method="POST">

        <img src=showImage.php?gim=1 width=500 height=150 alt="hell">

        <br>
        <hr>

        <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload(
        <100 KB): <input name="uploadedfile" type="file" />
        <br/>

        <input type="submit" value="submit" name="submit" />    
    </form>
<html>
/*showImage.php*/

<?php

    $con = mysql_connect("127.0.0.1:3306", "root", "");

    if (!$con) {
        die(“Could not connect: ” . mysql_error());
    }

    $DB = mysql_select_db("test", $con);
    $res = @mysql_query("select * from pix order by pic_id desc limit 1");

    if ($row = @mysql_fetch_assoc($res)) {

        $title = htmlspecialchars($row[pic_name]);
        $bytes = $row[pic_data];

    }

    header("Content-type: image/jpg");
    print $bytes;
    mysql_close();
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文