dongzhoutuo0883 2015-07-05 16:36
浏览 38
已采纳

php文件上传:在列表中使用unlink从服务器中删除元素

I have this php to upload a file, I want add a button at the right of each element of the list where if I click on I delete the element from the list and from the server. Is it possible? Thanks

<html>
<body>

<form action="" method="post" enctype="multipart/form-data">
    Select a file to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Load" name="submit">
</form>

<div>

    <?php
    $dir    = 'up/';
    $files = scandir($dir);
    echo 'uploaded files<br><hr>';
    $i = 1;
    foreach ($files as $key)
    {
        if ($i>2)
        {
            $j = $i-2;
            echo $j."&nbsp&nbsp<a href='up/".$key."'>".$key."</a><hr>";
        }
        $i++;
    }
    echo ' ';
    ?>

</div>
</body>
</html>

<?php
if (isset($_POST['submit']))
{
    echo '<center><h1>upload succesful!</h1></center>';
    $structure = 'up/';
    $target_file = $structure.basename($_FILES["fileToUpload"]["name"]);
    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}
?>
  • 写回答

3条回答 默认 最新

  • duanmao1975 2015-07-05 19:37
    关注

    You can do this by unlink the files that you need to do it.

    Here are the easy steps for you ;)

    Step 1 : Create an anchor tag which gives the path of the image/file

    <a href='index.php?delete=up/<?php echo $key?>'>Delete Now</a>

    Step 2 : On clicking the anchor tag, the page will be redirect to itself with the file's name

    Step 3 : Get the file and unlink (delete) it

    unset($_GET['delete']);

    Step 4 : Refresh the page once to have a fresh list of files.

    header("Refresh:0; url=".$url);

    Here's the Eval for you and Here's the code that you need to have

    <html>
    <body>
    
    <form action="" method="post" enctype="multipart/form-data">
        Select a file to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Load" name="submit">
    </form>
    
    <div>
    
        <?php
        error_reporting(0);
        session_start();
        $dir    = 'up/';
        $files = scandir($dir);
        echo 'uploaded files<br><hr>';
        $i = 1;
        foreach ($files as $key)
        {
            if ($i>2)
            {
                $j = $i-2;
                echo $j."&nbsp&nbsp<a href='up/".$key."'>".$key."</a>";
        if (isset($_GET['delete'])) 
        {
            unlink($_GET['delete']);
    
            $_SESSION['delete'] = $_GET['delete'];
            unset($_GET['delete']);
            $url = $_SERVER['SCRIPT_NAME'].http_build_query($_GET);
            header("Refresh:0; url=".$url);
        }
    ?>
       <a href='index.php?delete=up/<?php echo $key?>'>Delete Now</a>
                <hr>
                <?php
            }
            $i++;
        }
    
        ?>
    
    </div>
    </body>
    </html>
    
    <?php
    
    if (isset($_POST['submit']))
    {
        echo '<center><h1>upload succesful!</h1></center>';
        $structure = 'up/';
        $target_file = $structure.basename($_FILES["fileToUpload"]["name"]);
        move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制