首页 > 解决方案 > 使用php从服务器中删除文件和数据

问题描述

看在上帝的份上,我无法得到这份工作。我是 php/mysqli 的新手(我什至不知道了),我正试图让它为我所在的非营利组织工作。我再也不会这样做了。我已经尝试了 3 天,现在我不知道我在哪里了。

我正在尝试上传和下载文件,然后在我们不再需要时将其删除。

上传和下载作品。

我无法让删除功能工作。如果我的生活加深了……我会死的……

这是我的数据库连接:

    <?php
    $conn = new PDO( 'mysql:host=localhost;dbname=db_pdo_download', 'root', '');
    if(!$conn){
        die("Fatal Error: Connection Failed!");
    }
?>

这是上传/下载页面:

   <!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">

        </div>
    </nav>
    <div class="col-md-3"></div>    
    <div class="col-md-6 well">
        <h3 class="text-primary">Files</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <center>
            <form method="POST" action="upload.php" enctype="multipart/form-data">
                <div class="form-inline">
                    <input type="file" class="form-control" name="file" required="required"/>
                    <button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
                </div>
            </form>
        </center>
        <br />
        <table class="table table-bordered">
            <thead class="alert-info">
                <tr>
                    <th>File</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                    require 'conn.php';
                    $query = $conn->prepare("SELECT * FROM `file`");
                    $query->execute();
                    while($fetch = $query->fetch()){
                ?>
                <tr>
                    <td><?php echo $fetch['file']?></td>
                    <td>
                    <a href="download.php?file_id=<?php echo $fetch['file_id']?>" class="btn btn-primary">Download</a>

                    <!--Not Working-->
                        <a href="delete.php?file_id=<?php echo $fetch['file']?>" class="btn btn-primary">Delete</a>
                    <!--Not Working-->

                    </td>
                </tr>
                <?php
                    }
                ?>
            </tbody>
        </table>
    </div>
</body> 
</html>

这是我的上传功能:

    <?php
    require_once 'conn.php';

    if(ISSET($_POST['upload'])){
        $file_name = $_FILES['file']['name'];
        $file_temp = $_FILES['file']['tmp_name'];
        $file_size = $_FILES['file']['size'];
        $exp = explode(".", $file_name);
        $ext = end($exp);
        $time = date("d-m-Y");
        $name = $time."-".$file_name;
        $path = "uploads/".$name;

        if($file_size > 5242880){
            echo "<script>alert('File too large')</script>";
            echo "<script>window.location='index.php'</script>";
        }else{
            try{                
                if(move_uploaded_file($file_temp, $path)){
                    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    $sql = "INSERT INTO `file` (`file`, `location`) VALUES ('$name', '$path')";
                    $conn->exec($sql);
                }

            }catch(PDOException $e){
                echo $e->getMessage();
            }

            $conn = null;

            header('location:index.php');
        }
    }
?>

这是我的删除文件…..不工作:

 <?php
//connect to data 
require 'conn.php';



//what am I doing wrong here
//Trying to get the file to delete
$fileToDelete = $_GET['file_id'];

//Deleting that row using a prepared statement?
$sql = "DELETE FROM `file` WHERE `file_id` = :file";

//Prepare our DELETE statement.
$statement = $pdo->prepare($sql);

//The make that we want to delete from our cars table.
$makeToDelete = $fileToDelete;

//Bind our $makeToDelete variable to the paramater :make.
$statement->bindValue(':file', $makeToDelete);

//Execute our DELETE statement.
$delete = $statement->execute();
?>

标签: phpfilepdounlink

解决方案


推荐阅读