如何使用表格删除文件?

How to Delete File using Form?

我正在使用 OctoberCMS,基于 Laravel。

我正在尝试删除一个文件。我在文本框中输入文件名,然后按提交。

组件形式

<form method="POST" action="{{ url('/purge') }}">
    <input type="hidden" name="_handler" value="onPurge" />
    {{ form_token() }}
    {{ form_sessionKey() }}

    <input type="text" name="filename" /> 

    <input type="submit" name="submit" value="Purge" />  
</form>

组件PHP

public function onPurge(){
    $name = $_POST['filename'];

    if (!empty($_POST['submit'])) {
        $file->delete(storage_path("app/media/$name"));
    }
}

错误

Non-static method Illuminate\Database\Eloquent\Model::delete() should not be called statically

我试过了

public function onPurge(){
    $name = $_POST['filename'];

    if (!empty($_POST['submit'])) {
        $file = new Video();
        $file->delete(storage_path("app/media/$name"));
    }
}

(也有完整路径 /var/www/mysite/public/)

函数完成,没有错误,但文件没有删除。

您可以改用 php 函数取消链接。

if (!empty($_POST['submit'])) {
    $removed = unlink($path . $file);
}if(!$removed) {
   die('file could not be deleted');
}

你的清除函数应该是这样的,

public function onPurge(){
    $name = $_POST['filename'];
    $file_path = storage_path("app/media/$name");
    if(File::exists($file_path)){ // OR \File::exists($file_path)
     File::delete($file_path); // OR \File::delete($file_path)
    }
}

我觉得应该可以。

试一试。