TheSolarwin's avatar

Files doesn't uploads to storagefolder using storeAs function

Hi everyone! I am very new to Laravel everything went great until i tried to upload two files using form

FileControllerphp

public function store(Request $request)
    {
        $file = $request->all();
        $file['uuid'] = (string)Uuid::generate();
        $file['uuid2'] = (string)Uuid::generate();

        if ($request->hasFile('filename')) {
            $file['filename'] = $request->filename->getClientOriginalName();
            $request->filename->storeAs('files', $file['filename']);
        }
        if ($request->hasFile('filename2')) {
            $file['filename2'] = $request->filename2->getClientOriginalName();
            $request->filename2->storeAs('files', $file['filename2']);

        }
        File::create($file);

        return PostController::createPost($request);
    }

PostControllerphp

    public static function createPost(Request $request)
    {
        $post = new Post();
	    $post->title = $request->title;
	    $post->body = $request->body;
	    $post->save();
        return back()->with('post_created','Post created');
    }

Here you can see record in my table:

MariaDB [app]> select * from files;
+----+--------------------------------------+--------------------------------------+-----------------------------+--------------------------------------------+----------------------------------------------------------------+---------------------+---------------------+
| id | uuid                                 | uuid2                                | title                       | filename                                   | filename2                                                      | created_at          | updated_at          |
+----+--------------------------------------+--------------------------------------+-----------------------------+--------------------------------------------+----------------------------------------------------------------+---------------------+---------------------+
|  1 | ac68722097223a3fb7c7 | ac68738b3d1e5cc1fc | Testa title                 | Read Me First.txt                          | 10AppsManagerxls                                   | 2020-11-29 11:04:38 | 2020-11-29 11:04:38 |

When i submit a form, it creates a post as i expected, but the files are not uploaded on my server (folder storage, files) I checked documentation, folder rights, but still no luck

Any help would be highly appreciated

I can not post something when i use a full-stop, i got everytime this message "You may not include links the first day you sign up"

0 likes
4 replies
jlrdw's avatar

Perhaps upload them to an image folder underneath public, you could try that. Then use the asset helper to display them. In such a case as a symbolic link is not needed.

Just a suggestion.

Snapey's avatar

how about

if ($request->hasFile('filename')) {
            $file['filename'] = $request->file('filename')->getClientOriginalName();
            $request->file('filename')->storeAs('files', $file['filename']);
1 like
jlrdw's avatar

Also forgot to mention if you store image in another folder you can use move which laravel uses via a Symfony class, which in turn uses php move.

$file->move($destinationPath,  $yourfile);

Where $destinationPath is the path you are storing the file.

1 like
TheSolarwin's avatar

Your suggestions made me think that the problem is somewhere else I just added enctype to my form and now it works Thank you very much

            <form class="some-form" ..... enctype="multipart/form-data">

Please or to participate in this conversation.