dongqiao8417 2015-02-23 15:27
浏览 61

从PHP目录中删除文件

I have managed to get my file upload and download to a directory working, I just can't find anything on the delete option. I've written some code but it just dosn't work: this is my file upload:

<?php
if(isset($_FILES['file'])){
    $file = $_FILES['file'];
        //file properties

        $file_name = $file['name'];
        $file_tmp = $file['tmp_name'];
        $file_size = $file['size'];
        $file_error = $file['error'];
        //the file extension
        $file_ext = explode ('.', $file_name);
        $file_ext = strtolower(end($file_ext));
        //allowed extensions
        $allowed = array('txt', 'jpg');

        if(in_array($file_ext, $allowed)){
            if($file_error === 0){
                //allowed size
                if($file_size<= 2097152){
                    $file_name_new = $file_name;
                    $file_destination = 'files/' . $file_name_new;

                    if(move_uploaded_file($file_tmp, $file_destination)){
                        echo '<div class="errors"> Uploaded Succesfully </div>';

                    }

                }


            }
        }else echo ' <div class="errors"> Wrong file type </div>';  
}


?>

This is my display and download and delete:

<?php
$dir = "files/";
if ($opendir = opendir($dir)){
    while ($fileopen = readdir($opendir)){
        if ($fileopen != "." && $fileopen != ".." )
        echo "<a href='$dir$fileopen' download>$fileopen</br>";
        echo "<a href='deletefile.php?file=".$fileopen."'>DELETE";
    }

}
    else "could not open folder";

?>

and this is my deletefile.php :

$filename = $_GET['$fileopen']; //get the filename
unlink('final/files'.DIRECTORY_SEPARATOR.$filename); //delete it
header('location: upload.php'); //redirect back to the other page

?>

anyone knows how to fix it?

  • 写回答

1条回答 默认 最新

  • dqp99585 2015-02-23 15:57
    关注

    Try changing:

         unlink('final/files'.DIRECTORY_SEPARATOR.$filename);
    

    To this:

         unlink('files'.DIRECTORY_SEPARATOR.$filename);
    

    You aren't working in the files directory when you are deleting.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题