赞
踩
- <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js">
- </script>
- <script src="./aa.js"></script>
- <form method="post" action="" enctype="multipart/form-data" id="form">
- <input type="file" name="file" onchange="aj()" id="bnm">
- <input type="submit" name="zxing">
- <img src="https://img2.baidu.com/it/u=1171791560,3850385956&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
- alt="" id="a">
- </form>
- <?php
- // 连接
- $servername = "localhost";
- $username = "root";
- $password = "123456";
- $dbname = "myDB";
- $conn = new mysqli($servername, $username, $password,$dbname);
- // 检测连接
- if ($conn->connect_error) {
- echo "<script>alert('小皮是不是没有开?')</script>";
- }
-
- // 允许上传的图片后缀
- $a = array("gif", "jpeg", "jpg", "png");
- // 分解后缀
- $b = explode(".", $_FILES["file"]["name"]);
- // 获取文件后缀名
- $c = end($b);
- $name = '二';
- $passwordss = '123456';
- // $f = $_POST['file'];
- // $file = $_POST['file'];
- // 插入
- if (array_key_exists('zxing',$_POST)) {
- $d = "upload/" . $_FILES["file"]["name"];
- // copy($file,$_FILES["files"]);
- echo "<br/>";
- echo $f;
- echo "<br/>";
- if ((($_FILES["file"]["type"] == "image/gif")
- || ($_FILES["file"]["type"] == "image/jpeg")
- || ($_FILES["file"]["type"] == "image/jpg")
- || ($_FILES["file"]["type"] == "image/pjpeg")
- || ($_FILES["file"]["type"] == "image/x-png")
- || ($_FILES["file"]["type"] == "image/png"))
- && ($_FILES["file"]["size"] < 204800) // 小于 200 kb
- && in_array($c, $a))
- {
- if ($_FILES["file"]["error"] > 0)
- {
- echo "66666";
- }
- else
- {
- // 判断上传的东西存不存在
- if (file_exists($d))
- {
- echo 1;
- }
- else
- {
- // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
- move_uploaded_file($_FILES["file"]["tmp_name"], $d);
- echo "名字".$_FILES["file"]["name"];
- echo "<br/>";
- echo "类型".$_FILES["file"]["type"];
- echo "<br/>";
- echo "大小".$_FILES["file"]["size"];
- echo "<br/>";
- echo "位置".$_FILES["file"]["tmp_name"];
- echo "<br/>";
- $fc = "./upload/" .$_FILES["file"]["name"];
- echo $fc;
- $sqls = "INSERT INTO wangmou (name, password,img)VALUES('$name','$passwordss','$fc')";
- if ($conn->query($sqls) === TRUE) {
- echo "<center> 插入成功!</center>";
- }else{
- // echo "失败";
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- }
- }
- }
- else
- {
- echo 2;
- }
-
- }
-
-
-
-
-
- ?>
- function aj() {
- let forms = new FormData(document.getElementById('form'));
- console.log(1);
- console.log(forms);
- $.ajax({
- type: "POST",
- url: "./b.php",
- data: forms,
- // dataType: 'json',
- Cache: false,
- processData: false,
- contentType: false,
- success: function (res) {
- console.log(res);
- $("#a").attr("src", res);
- },
- error: function (res) {
- console.log(res);
- }
- })
- }
- // 向数据库中插入图片
- $imgfile=$_FILES['imgfile'];
- $submitbtn=$_POST['submitbtn'];
- if($submitbtn=='OK' and is_array($imgfile))
- {
- $name=$imgfile ['name']; // 取得图片名称
- $type=$imgfile ['type']; // 取得图片类型
- $size=$imgfile ['size']; // 取得图片长度
- $tmpfile=$imgfile ['tmp_name']; // 图片上传上来到临时文件的路径
- if ($tmpfile and is_uploaded_file ($tmpfile)) // 判断上传文件是否为空,文件是不是上传的文件
- {
- // 读取图片流
- $file=fopen($tmpfile,"rb");
- $imgdata=bin2hex (fread ($file,$size)); //bin2hex () 将二进制数据转换成十六进制表示
- fclose($file);
- $mysqli=new mysqli("localhost","root","123456","myDB"); // 连接数据库函数
- // 插入出数据库语句,图片数据前要加上 0x,用于表示 16 进制数
- if(new mysqli("insert into images(name,type,image) values('".$name."','".$type."',0x".$imgdata.")"))
- echo "<center> 插入成功!<br><br><a href='disimage.php'> 显示图片 </a></center>";
- else
- echo "<center> 插入失败!</center>";
- mysql_close();
- }
- else
- echo "<center> 请先选择图片!<br><br><a href='image.html'> 点此返回 </a></center>";
- } else
- echo "<center> 请先选择图片!<br><br><a href='image.html'> 点此返回 </a></center>";
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。