wardaddy's avatar

Laravel 5.4 - how to put file in the database directory

I'm working on the database restore menu for an application I created, I'm using sqlite for my database, and I want to put my previously backed up database file to the database directory

backupAndRestore.blade.php

<form method="POST" action="/admin/backupAndRestore/restore" enctype="multipart/form-data">
                {{ csrf_field() }}
                <div class="file-field input-field">
                  <div class="btn">
                    <span><i class="material-icons">file_upload</i></span>
                    <input type="file" name="database" accept=".sqlite">
                  </div>
                  <div class="file-path-wrapper">
                    <input type="text" class="file-path" placeholder="Example: file_name.sqlite" readonly>
                  </div>
                </div>
                <button type="submit" class="btn">restore</button>
              </form>

Route

Route::post('/admin/backupAndRestore/restore', 'DatabaseController@restore');

DatabaseController.php

public function restore(Request $request)
    {
        $database = $request->database;
        $file_name = 'database2.sqlite';
        $path = database_path($file_name);

        file_put_contents($path, $database);

        return $path;
    }

my code works, the file is stored in the database directory, the problem is when I backup my database file size is 22kb, but when I restore it and I check the database directory, the file is 1kb size and when I open using sublime file contents as shown below

Images

which I expect as shown below

Images

0 likes
8 replies
jryd's avatar

file_put_contents() takes the filename/path and the data you want to write.

Your restore method is actually writing the temporary location of where the database is uploaded to, as a string, to the location you specify.

That's why the file turns up in the right location, but contains a string as opposed to the actual database.

It depends how elegant you want your solution to be. One solution could be to do the following:

First - Create a new 'disk' to store your databases - this will also make it easier later on to retrieve the files as required

//config/filesystems.php

'databases' => [
    'driver' => 'local',
    'root' => database_path(),
],


Second - Now you can just store it off the $request object:

public function restore(Request $request)
{

    //Store the file in the 'databases' storage 'disk' using the name of the uploaded file
    $path = $request->file('database')->store($request->database, 'databases');

    return $path;
}

More information - https://laravel.com/docs/5.5/filesystem#file-uploads specifically, 'Specifying A Disk'

1 like
jryd's avatar

@wardaddy your error suggests that you don't have permission to write to that folder, or at least the user running the Laravel application doesn't.

Resolve the permission issue and this will work perfectly.

gator's avatar

@wardaddy the error message still refers to your earlier code - using file_put_contents (DatabaseController line 25)

You are not yet using the code suggested by @jryd (which should work as expected).

jryd's avatar

Can you please show us the full whoops error message, showing us the line of code that failed too.

shez1983's avatar

how many times are you expecting the database to go down? mine has never gone down in the last 2 years.. (i am still making backups) but dont have a restore functionality liek you are trying to build.. if you are expecting lots then you are doing something wrong ;)

if your db goes down, your site will be non-functional anyway.. and i assume your 'restore' functionality will be available under 'admin panel' which will NEED an admin user.. which your app doesnt know because DB is down... ;)

to start off with upload the spaties/backup package.. and then run the backup that takes the backup every 30/60/whateevr minutes... and if your db goes down you will have to manually re-upload... (set some monitos (I believe) that checks your site every 5 minutes or so)

wardaddy's avatar
wardaddy
OP
Best Answer
Level 1

I have solved my problem with the code below, hopefully can help others

    public function restore(Request $request)
    {
        $file = $request->file('database');
        Storage::disk('base')->putFileAs('database', $file, 'database.sqlite');
        
        return back();
    }

Please or to participate in this conversation.