search
Homephp教程php手册php上传图片存入数据库示例分享

这篇文章主要介绍了php上传图片存入数据库示例,需要的朋友可以参考下

大部分人的图片上传都是保存一个路径到数据库,这样在插入时确实快,,也符合web的特点,但是在删除时就很麻烦,需要找到文件并删除,该代码能够把代码直接存入数据库,删除时一并删除。请注意:这样的话数据库大小会激增,请酌情使用

表结构 

复制代码 代码如下:


CREATE TABLE `upload` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `type` varchar(20) NOT NULL,
  `data` mediumblob NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

index.html

复制代码 代码如下:



   


    <br>        Post-Image<br>   



   
   



post.php

复制代码 代码如下:


if ($_FILES["file"]["error"] > 0)
{
    echo "Error: " . $_FILES["file"]["error"] . "
";
}
else
{
    $type = $_FILES["file"]["type"];
    $size = $_FILES['file']['size'];
    $tmp=$_FILES["file"]["tmp_name"];
    $fp = fopen($tmp,'rb');
    $data = bin2hex(fread($fp,$size));
    $dsn='mysql:host=localhost;dbname=test';
    echo '

';<br>    try{<br>        $pdo = new PDO($dsn,'root','root');<br>        $pdo->exec("INSERT INTO `upload`(`type`,`data`) values ('$type',0x$data)");<br>        $id = $pdo->lastInsertId();<br>        echo 'upload success!<a href="view.php?id='.%24id.'">View</a>';<br>        $pdo = null;<br>    }catch (PDOException $e){<br>        echo $e->getMessage();<br>    }<br>    echo '
';
    fclose($fp);
}

view.php

复制代码 代码如下:


$id = $_GET['id'];
if(is_numeric($id)){
    $dsn='mysql:host=localhost;dbname=test';
    try{
        $pdo = new PDO($dsn,'root','root');
        $rs = $pdo->query('select * from `upload`  where `id`='.$id);
        $row = $rs->fetchAll();
        $data = $row[0];
        header("Content-Type:${data['type']}");
        echo $data['data'];
        $pdo = null;
    }catch (PDOException $e){
        echo $e->getMessage();
    }
}else{
    exit();
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use