zoukankan      html  css  js  c++  java
  • php之上传图片及传数据到mysql

    index.html

    <form action="php.php" method="post" enctype="multipart/form-data">
            <input type="file" name="filename"/>
            <input type="submit" value="上传文件"/>
    </form>

    php.php

    <?php

    try {
        $file = $_FILES['filename'];
        if (!$file['error'] == 0) {
            throw new Exception('上传文件出错');
        }
    //文件来源安全性(文件上传白名单)
        if (!$file['tmp_name']) {
            throw new Exception('您的图片来源不安全');
        }
    //文件目录
        $dir = 'upload/' . date('ym/');
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }
        //文件上传大小
        if ($file['size'] > 80000000) {
            throw new Exception('文件不得超过80000000M');
        }
    //文件名
        $name = $file['name'];
        $ext = substr($name, strrpos($name, '.'));
        if (!preg_match('/(.jpg)|(.png)|(.gif)$/', $ext)) {
            throw new Exception('图片格式错误');
        }
        $newname = md5(time() . rand(0, 999999999) . rand(111, 9999)) . $ext;
        $filenamea = $file['tmp_name'];
        move_uploaded_file($filenamea, $dir . $newname);
    //    数据库连接
        $link = new mysqli('localhost', 'root', 'password', 'image');
        if ($link->connect_errno) {
            unlink($dir . $newname);
            throw new Exception('数据库连接失败');
        }
        $sql = 'INSERT INTO images (`name`,url,`time`) VALUES ("' . $name . '","' . $dir . $newname . '",' . time() . ')';
        var_dump($sql);
        $res = $link->query($sql);
        if (!res) {
            echo '失败';
        }
    } catch (Exception $ex) {
        echo $ex->getMessage();
    }
  • 相关阅读:
    15 手写数字识别-小数据集
    14 深度学习-卷积
    13-垃圾邮件分类2
    12.朴素贝叶斯-垃圾邮件分类
    11.分类与监督学习,朴素贝叶斯分类算法
    9、主成分分析
    7.逻辑回归实践
    8.特征选择,过滤式
    6.逻辑回归
    5.线性回归算法
  • 原文地址:https://www.cnblogs.com/wdxue/p/8340198.html
Copyright © 2011-2022 走看看