Dylan Heitz Dylan Heitz - 5 months ago 23
PHP Question

Delete files/uploads when delete User

I have my Users table (with name, email, password, etc...) and the users can upload pictures, files or videos.

Their pictures are stored in specific path (for example

/myproject/app/public/uploads/01/picture.jpg
)

I wondered if the files uploaded by the users can be deleted when the row of the user is deleted.

The big issue is that I want to delete the data that the users have uploaded when I do
php artisan migrate:refresh --seed


I tried this on the migration file
2014_..._create_users_table.php
:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

use File;

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{

Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
// Deleting ALL the data that the users have uploaded
$dir = public_path(). "/uploads/";
File::deleteDirectory($dir, true);
}
}


But I have the following error when I try to execute
php artisan migrate:refresh --seed


[ErrorException]
The use statement with non-compound name 'File' has no effect


Thank you for your help !

Answer

Change use File; to use Illuminate\Support\Facades\File as File;