upload

来源:互联网 发布:mac 翻墙 知乎 编辑:程序博客网 时间:2024/05/16 10:42
upload.html
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body><form action="upload.php" method="post" enctype="multipart/form-data">    <input type="file" name="img">    <input type="submit" value="上传图片"></form></body></html>
upload.php
<?php/** * Created by PhpStorm. * User: dllo * Date: 16/8/17 * Time: 下午2:42 */require_once "common.php";header("Content-type:text/html;charset=utf-8");//var_dump($_FILES);$img = $_FILES["img"];// 上传成功if ($img["error"] == 0){    $types = explode("/",$img["type"]);    if ($types[0] == "image"){        // 文件名14xxxx.png        $name = time().".".$types[1];        // 移动图片        if (move_uploaded_file($img["tmp_name"],"img/".$name)){//            var_dump(pathinfo($_SERVER["HTTP_REFERER"]));            $imgPath = pathinfo($_SERVER["HTTP_REFERER"])["dirname"]."/img/".$name;            $sql = "INSERT INTO image (id,imgPath) VALUES (NULL,'{$imgPath}')";            mysql_query($sql);            if (mysql_insert_id() > 0){                echo '{"err":0,"msg":"添加成功"}';            }else{                echo '{"err":1,"msg":"添加失败"}';            }        }    }}

0 0