Voyager: How to change file upload location?

Created on 19 Dec 2016  路  16Comments  路  Source: the-control-group/voyager

  • Laravel Version: 5.3
  • Voyager Version: latest
  • PHP Version: 5.6
  • Database Driver & Version:

Description:

I am wondering, since I am using Azure web server, I am not allowed to create symlinks between storage/app/public to public/storage...

How can I change the upload location for media files on voyager to upload to somewhere in my main public directory like wwwroot/public/media/

So, right now it uploads files into wwwroot/sorage/app/public/...

Please help since the URL's generated are not accessible...

Most helpful comment

@pranjalkumar I managed to do it by updating 2 files:

  1. config/filesystems.php
    'disks' => [
    'custom' => [
    'driver' => 'local',
    'root' => storage_path('../public/storage'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
    ],
    ],
  2. config/voyager.php
    'storage' => [
    'disk' => 'custom',
    ],

Hope this helps, let me know.

All 16 comments

In my case I have a subfolder in my website. To enter the system one should go to:

www.mywebsite.com/system/admin.

If I upload a file, the generated url is:

 http://www.mywebsite.com/storage/services/December2016/KJTl0xfFa7Rnec5QLuii.jpg

where services is a table I've created via admin panel... but the file is not being uploaded to this location. When I access it I've got a 404 error. For the older files I've uploaded locally, if I type:

http://www.mywebsite.com/system/storage/

I can find them, under each model folder... but the new files are not being uploaded to there. They're being uploaded under

/laravel/storage/app/public/

If I change the path and try to use /../../public_html/system/ on the config/voyager.php file, I've got an error Path is outside of the defined root

My laravel folder is on the same level as the public_html folder, which is where my system/index.php file is located... how can I fix the route to change the location to where the uploaded files are actually stored? Ideally, I'd like to store them under system/storage folder or at least to get the routes to find the files using the correct path.

You can do this by making a symbolic link from /public/media to /storage/app/public and remove the old one at /public/storage.

I can do it only via Terminal then upload the project again??

I'm not entirely sure, I think you might need to do it on the server.

" I am not allowed to create symlinks between storage/app/public to public/storage..."

Why don't you read my issue entirely...

That #397 you linked is my original question...

I cannot create a symlink with my Azure setup, it is not allowed.

Is there a way to change the upload location entirely...

I see @cadmicro.

Will look into this.

I am working on a workaround... but its very dirty... I have modified the VoyagerMediaController.php to work with a custom folder location...

I still need to fix the delete_file_folder and move_file functions however.

    public function files(Request $request)
    {
        $folder = $request->folder;
        if ($folder == '/') {
            $folder = '';
        }
        $dir = $this->directory.$folder;

        $dir = base_path().'/public/storage/'.$dir.'/';

        return response()->json([
            'name'          => 'files',
            'type'          => 'folder',
            'path'          => $dir,
            'folder'        => $folder,
            'items'         => $this->getFiles($dir),
            'last_modified' => 'asdf',
        ]);
    }
   private function getFiles($dir)
   {
       $files = [];

       $storageFiles = File::files($dir);
       $storageFolders = File::directories($dir);

       foreach ($storageFiles as $file) {

          $file = str_replace("/\\","/",$file);

           $files[] = [
               'name'          => (strpos($file, '/') > 1 ? str_replace('/', '', strrchr($file, '/')) : $file),
               'type'          => File::mimeType($file),
               'path'          => str_replace("public/storage/public/", "storage/public/",str_replace(base_path(),"", $file)),
               'size'          => File::size($file),
               'last_modified' => File::lastModified($file),
           ];
       }
       foreach ($storageFolders as $folder) {
           $files[] = [
               'name'          => str_replace('\\','', strpos($folder, '/') > 1 ? str_replace('/', '', strrchr($folder, '/')) : $folder),
               'type'          => 'folder',
               'path'          => Storage::disk(config('filesystem.default'))->url($folder),
               'items'         => '',
               'last_modified' => '',
           ];
       }
       return $files;
   }
    public function new_folder(Request $request)
    {
        $new_folder = $request->new_folder;
        $success = false;
        $error = '';
        $new_folder = str_replace(base_path(), '', $new_folder);
        $new_folder = str_replace('/public/storage/public/', '', $new_folder);
        $new_folder = base_path().'/public/storage/public/'.(str_replace('/storage/', '/',$new_folder));

        if (File::exists($new_folder)) {
            $error = 'Sorry that folder already exists, please delete that folder if you wish to re-create it';
        } else {
            if (File::makeDirectory($new_folder, 0775, true)) {
                $success = true;
            } else {
                $error = 'Sorry something seems to have gone wrong with creating the directory, please check your permissions';
            }
        }

        return compact('success', 'error');
    }

I will work on this and make it into a configuration later.

Thanks, @marktopper You've been helping me a lot on my work! I really appreciate your efforts and I hope I can contribute somehow with Voyager too. Thank you very much!

Hey, have you managed to implement this already, please?

Hey did anyone find a solution for this as I am also facing similar problem.

@pranjalkumar I managed to do it by updating 2 files:

  1. config/filesystems.php
    'disks' => [
    'custom' => [
    'driver' => 'local',
    'root' => storage_path('../public/storage'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
    ],
    ],
  2. config/voyager.php
    'storage' => [
    'disk' => 'custom',
    ],

Hope this helps, let me know.

How can I change the maximum upload file size in public storage?
It seems like max. 2M, right?
Thanks.

Closed since this is fixed by making it possible to change the storage driver.

@ainvention: That limit is a server-side configuration, often found in php.ini

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.

Was this page helpful?
0 / 5 - 0 ratings