[推荐]任何数据存入mysql,及从中提取的程序

楼主
[推荐]任何数据存入mysql,及从中提取的程序
<?php
// 文件名:filedb.php 将文件储存到数据库中
/*
将文件上传存入 MySQL 数据库的例子
数据表的结构如下:
CREATE TABLE files (
  id int(4) NOT NULL auto_increment,
  filename varchar(64),
  filesize int(16),
  filetype varchar(64),
  filecontent longblob,
  remark text,
  PRIMARY KEY  (id)
);
*/
// 设定系统参数变量,根据需要修改
$DB_SERVER = "127.0.0.1"; # 数据库连接字
$DB_USER = "root"; # 用户名
$DB_PASS = ""; # 密码
$DB_NAME = "article"; # 数据库名
$TABLE_NAME = "files"; # 数据表名
$HANDLER_SCRIPT = "mysql数据库显示文件.php"; # 处理数据的脚本文件名
$PHP_SELF=$_SERVER['PHP_SELF'];
?>
<html>
<head>
<title>将文件存入数据库</title>

<style>
caption { background-color: #E6F4FF; font-size:14px}
td      { background-color: #E6F4FF; font-size:12px}
th      { background-color: #FFCC00; font-size:12px}
div     {font-size:12px}
</style>
</head>
<body>
<form enctype='multipart/form-data' method='post'>
<table cellspacing=1 cellpadding=5  >
<caption>文件存入数据库管理器</caption>
<input type='hidden' name='MAX_FILE_SIZE' value=10489760>
 <tr>
  <td>上传文件:</td>
  <td><input type='file' name='userfile'></td>
 </tr>
 <tr valign=top>
  <td>文件说明:</td>
  <td><textarea name='description' rows='4' cols='40'></textarea></td>
 </tr>
 <tr>
  <td colspan=2 align=center><input type='submit' name='submit' value=' 上传 '>
 </tr>
</table>
</form>
<?php
if (isset($_POST["submit"])) // 表单被提交后执行以下代码
{
    if (!is_uploaded_file($_FILES['userfile']['tmp_name'])) // 检查文件是否上传成功
    {
        die("文件上传失败!");
    }
    $link = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS) // 连接数据库
    or die("连接数据库失败!");
    mysql_select_db($DB_NAME);
    $filename = addslashes($_FILES['userfile']['name']); # 为了正确保存入数据库,
    $filesize = addslashes($_FILES['userfile']['size']); # 对所有的插入数据都要
    $filetype = addslashes($_FILES['userfile']['type']); # 进行 addslashes 操作
    // 读入上传的文件并做预处理 #
    $tmp_name = $_FILES['userfile']['tmp_name'];
    $fd = fopen ($tmp_name, "rb");
    $contents = fread ($fd, filesize ($tmp_name));
    fclose ($fd);
    $filecontent = addslashes($contents);
    $tmp_name = $_FILES['userfile']['tmp_name'];
    $fd = fopen ($tmp_name, "rb");
    $contents = fread ($fd, filesize ($tmp_name));
    fclose ($fd);
    $filecontent = addslashes($contents);
    $remark = addslashes($_POST['description']);

    $query_string = "INSERT INTO $TABLE_NAME VALUES ('', '$filename', '$filesize', '$filetype', '$filecontent', '$remark')";
    $result = mysql_query($query_string) or die("数据插入失败!"); # 进行数据插入操作
    echo "<div id='info'>保存成功!
\n";
    echo "文件名 :{$_FILES['userfile']['name']}
\n";
    echo "文件大小:{$_FILES['userfile']['size']} 字节
\n";
    echo "文件类型:{$_FILES['userfile']['type']}</div>\n";
    echo   "<input type=\"submit\" value=刷新 onclick=\"window.location='$PHP_SELF'\">";
}
echo "
<table border=2 bordercolor=#055AA0 cellspacing=0 width=840  cellpadding=0 style=\"border-collapse: collapse ;LEFT: 0px; WORD-WRAP: break-word; word-break:break-all\" >
<tr>
 <th width=>ID</th>
 <th>文件名</th>
 <th>文件大小</th>
 <th>文件类型</th>
 <th>说明</th>
 <th>连接</th>
 <th>删除</th>
</tr>\n";
$link = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS) // 显示数据库已有记录
or die("连接数据库失败!");
mysql_select_db($DB_NAME);
$query_string = "SELECT * FROM $TABLE_NAME ORDER BY id"; # 取出所有记录,无分页显示功能
$result = mysql_query($query_string) or die("查询出错!");
while ($row = mysql_fetch_array($result))
{
    $remark = nl2br(htmlspecialchars($row["remark"])); # 为了正确显示说明,需要作预处理
    echo "<tr>
 <td>$row[id]</td>
 <td>$row[filename]</td>
 <td align=right>$row[filesize]</td>
 <td>$row[filetype]</td>
 <td>$remark</td>
 <td nowrap><a href='$HANDLER_SCRIPT?action=show&id=$row[id]' target=_blank>点击查看</td>
 <td nowrap><a href='#' OnClick='window.open(\"$HANDLER_SCRIPT?action=del&id=$row[id]\");location.href=\"$_SERVER[PHP_SELF]\";'>点击删除</td>
</tr>\n";
}
echo "</table>\n";

?>
</body>
</html>
1楼
<?php
// 文件名:filehandle.php 用来显示文件或删除记录
// 系统参数变量
$DB_SERVER = "127.0.0.1"; # 数据库连接字
$DB_USER = "root"; # 用户名
$DB_PASS = ""; # 密码
$DB_NAME = "article"; # 数据库名
$TABLE_NAME = "files"; # 数据表名

if (!empty($_GET["action"])) // 判断页面合法性
{
    if ($_GET["action"] == "show") // 页面功能为显示文件
    {
        if (!empty($_GET["id"])) // 取得 id 号
            $id = $_GET["id"];
        else
            $id = 1; # 如果 id 为空则定义为 1
        $link = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS)
        or die("连接数据库失败!");
        mysql_select_db("$DB_NAME");
        // 从数据库中取出文件名,文件类型以及文件本身
        $query_string = "SELECT filename, filetype, filecontent FROM $TABLE_NAME WHERE id = '$id'";
        $result = mysql_query($query_string) or die("查询出错!");
        if (mysql_num_rows($result) == 0) // 判断查询是否成功
            die("查无记录!");
        $row = mysql_fetch_array($result); # 取得查询结果
        $filecontent = $row["filecontent"]; # 取得文件内容
        $filetype = $row["filetype"]; # 取得文件类型
        $filename = $row["filename"]; # 取得文件名
        header("Content-Type: $filetype"); # 输出 HTTP 头信息,文件的 MIME 类型
        header("Content-Disposition: inline; filename=\"$filename\""); # 这样可以给出文件#    
        header("Content-Disposition: attachment; filename=\"$filename\""); # 这样可以迫使 IE 下载文件而不显示
        echo $filecontent; # 输出文件
    }
}

?>
2楼
<?php
// 设定系统参数变量,根据需要修改
$DB_SERVER = "127.0.0.1"; # 数据库连接字
$DB_USER = "root"; # 用户名
$DB_PASS = ""; # 密码
$DB_NAME = "article"; # 数据库名
$TABLE_NAME = "files"; # 数据表名

$link = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS) // 连接数据库
or die("连接数据库失败!");
mysql_select_db($DB_NAME, $link);
{
    $action = $_GET['action'];
    $id = $_GET['id'];
}

if (!$action)echo "<script>window.location='向mysql插入任何数据.php'</script>";

if ($action == "del")
{
    $del_sql = mysql_query("delete from $TABLE_NAME where `id`='$id' ");
    if($del_sql) {
    echo "<script>\nalert(\"成功删除该记录!\");\nwindow.close();\n</script>";
    exit;
    }
    else
        echo "<script>\nalert(\"删除记录出错!\");\nwindow.close();\n</script>";
     
}

if ($action == "show")
{
    $show_sql = mysql_query("select * from $TABLE_NAME where `id`='$id' ") or die("无法显示文件");
    $show_result = mysql_fetch_object($show_sql);
    $mine = $show_result->filetype;
    Header("Content-type: $mine");
    // echo   "$mine ";
    if (eregi("image", $mine))
    {
        echo $show_result->filecontent;
    }
    else
    {
        $text = htmlspecialchars($show_result->filecontent);
        $text = nl2br($text);
        $text = strtr($text, array('<' => '<', """ => "\"", "<br />" => "", "&" => "&", ">" => ">"));
        echo "$text";
    }
}

?>

电脑版 Page created in 0.0312 seconds with 2 queries.