How to Delete File using Form?
Ad
I'm using OctoberCMS, based on Laravel.
I'm trying to delete a file. I enter the filename in a text box and press submit.
Component Form
<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>
Component PHP
public function onPurge(){
$name = $_POST['filename'];
if (!empty($_POST['submit'])) {
$file->delete(storage_path("app/media/$name"));
}
}
Error
Non-static method Illuminate\Database\Eloquent\Model::delete() should not be called statically
I tried
public function onPurge(){
$name = $_POST['filename'];
if (!empty($_POST['submit'])) {
$file = new Video();
$file->delete(storage_path("app/media/$name"));
}
}
(also with full path /var/www/mysite/public/)
The function completes, No error, but the file does not delete.
Ad
Answer
Ad
Your purge function should be like,
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)
}
}
I feel it should work.
Give it a try.
Ad
source: stackoverflow.com
Related Questions
Ad
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad