php上传图片至文件夹并把路径保存到数据库

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<link rel="stylesheet" type="text/css" href="skin/css/base.css">
<body>
<?php
if ($_GET ['action'] == 2) {
    if ((($_FILES ["file"] ["type"] == "image/gif") || ($_FILES ["file"] ["type"] == "image/jpeg") || ($_FILES ["file"] ["type"] == "image/pjpeg")) && ($_FILES ["file"] ["size"] < 20971520)) {
        if ($_FILES ["file"] ["error"] > 0) {
            echo "错误: " . $_FILES ["file"] ["error"] . "<br />";
        } else {
            if (file_exists ( "upload/" . $_FILES ["file"] ["name"] )) {
                echo "<script>alert('" . $_FILES ["file"] ["name"] . " 已经存在!')</script> ";
            } else {
                move_uploaded_file ( $_FILES ["file"] ["tmp_name"], "upload/" . $_FILES ["file"] ["name"] );
                include_once ("conn.php");
                include_once ("comm.php");
                $picpath = "upload/" . $_FILES ["file"] ["name"];
                $ptime = date ( "y-m-d h:i:s", time () );
                $insql = insert ( "pics", "p_path,p_time", "'$picpath','$ptime'" );
                //echo $insql;
                if (mysql_query ( $insql )) {
                    echo "<script>alert('上传成功!')</script>";
                } else {
                    echo "<script>alert('上传失败!')</script>";
                }
            }
        }
    } else {
        echo "<script>alert('无效的图片格式或者超出最大限制大小!');</script>";
    }
}
?>
<table width="98" align="center" border="0" cellpadding="3"
    cellspacing="1" bgcolor="#CBD8AC"
    style="margin-bottom: 8px; margin-top: 8px;">
    <tr>
        <td background="skin/images/frame/wbg.gif" bgcolor="#EEF4EA"
            class='title'>上传图片</td>
    </tr>
    <tr bgcolor="#FFFFFF">
        <td>
        <table width="80" border="0" align="center" cellpadding="0"
            cellspacing="0">
            <tr>
                <td>
                <form action="?action=2" method="post" enctype="multipart/form-data">
                <label for="file"><br />
                文件名:</label> <input type="file" name="file" id="file" /> <input
                    type="submit" name="submit" value="上传图片" /> <span
                    style="color: #F00;">(只能上传jpg或者gif格式的图片,且大小不能大于20兆)</span></form>
                </td>
            </tr>
        </table>
        <br />
        </td>
    </tr>
</table>
</body>
</html>


by 雪洁 2009-12-07 18:32:12 13950 views

1 Comments

游客 2012-08-03 11:26:58
Stellar work there everyone. I'll keep on reandig.
我来说几句

相关文章