PHP&MySQL图片上传及管理

PHP&MySQL图片上传及管理

图片上传

HTML表单代码

<form action="uploadpic.php" method="post" enctype="multipart/form-data">
    <label for="exampleInputEmail1">标题label>
    <input type="text" name="title" placeholder="请输入标题">
    <label for="exampleInputFile">文件上传label>
    <input type="file" name="mypic" id="exampleInputFile">
    <p>文件小于2Mp>
    <button type="submit">确定button>
form>

如图
PHP&MySQL图片上传及管理_第1张图片


uploadpic.php代码

上传图片完成后,$_FILES[‘mypic’]是一个数组,储存着上传文件的类型、大小、文件名、临时文件夹位置以及error信息,返回的error为0代表上传成功,其他数字代表上传失败的原因。

$link=new mysqli("localhost","root","password","DATABASE_NAME");
$title=$_POST['title'];
$filename=$_FILES['mykpic']['name'];
$type=$_FILES['mykpic']['type'];
$tmp_name=$_FILES['mykpic']['tmp_name'];
$error=$_FILES['mykpic']['error'];
$size=$_FILES['mykpic']['size'];
//$destination是文件路径;
//下面用move_uploaded_file将上传的文件从临时文件夹移动到$destination的位置;
$destination="img/".$filename;
if($error==UPLOAD_ERR_OK){
    if(is_uploaded_file($tmp_name)){
        if(move_uploaded_file($tmp_name, $destination)){
            $mes="上传文件成功";
            $sql="insert into pic values ('{$title}','{$filename}','{$type}','{$destination}','{$size}','{$id}')";
            mysqli_query($link, $sql);
        }else{
            $mes="上传文件失败";
        }
    }
}else{
    switch ($error){
        case 1:
            $mes="超过了配置文件上传文件的大小";
            break;
        case 2:
            $mes="超过了表单设置上传文件的大小";
            break;
        case 3:
            $mes="文件部分被上传";
            break;
        case 4:
            $mes="没有文件被上传";
            break;
        case 6:
            $mes="没有找到临时目录";
            break;
        case 7:
            $mes="文件不可写";
            break;
        case 8:
            $mes="由于PHP的扩展程序中断了文件上传";
            break;

    }
}
echo "";
echo"";

图片管理

HTML&PHP表单代码

<form action="updatepic.php" method="post">
    <table>                           
        <tr>
            <th>标题th>
            <th>查看图片th>
            <th>删除th>
        tr>
        
            function getalltable($table_name){
                $link=new mysqli("localhost","root","password","DATABASE_NAME"); 
                $sql="select * from $table_name";
                $result=mysqli_query($link, $sql);
                return $result;
            }
            $i=1;
            $result=getalltable("pic");
            while($rows=mysqli_fetch_row($result)){
                $_SESSION['mypicname{$i}']=$rows[1];
                $title=$rows[0];
                $destination=$rows[3];
                //中括号中的数字取决于储存上传文件数据的数据库中列的顺序;
                echo "
                          ".
                         "点击查看".
                         "删除
                     ";
                $i++;
            }?>
    table>
    <input type="submit" value="确认修改">input>
form>

如图
这里写图片描述

updatepic.php代码

function allrows($table_name){
    $link=new mysqli("localhost","root","password","DATABASE_NAME");
    $sql="select count(*) from $table_name";
    $allrows_one=mysqli_query($link, $sql);
    $allrows=mysqli_fetch_array($allrows_one,$allrows_one_type=MYSQLI_ASSOC);
    return $allrows;
}
$i=1;
while($i<=allrows("inkpic")['count(*)']){
    $mypicname=$_SESSION["mypicname{$i}"];
    $title=$_POST["title{$i}"];
    $sql="update inkpic set title='{$title}' where name='{$mypicname}'";
    mysqli_query($link, $sql);
    $i++;
}
echo "";
echo "";

删除代码

$link=new mysqli("localhost","root","password","DATABASE_NAME");
$destination=$_GET['destination'];
$sql="delete from inkpic where destination='{$destination}'";
unlink($destination);
mysqli_query($link, $sql);
echo"";

你可能感兴趣的:(PHP&MySQL图片上传及管理)