Bajongskie
Bajongskie

Reputation: 463

Laravel - File Uploading issues using a virtual host

I have a problem regarding uploading a file to my server with laravel in a virtual host.

Here's the case: when I try to upload an have a link to the absolute path like this:

if ($file->move( 'laravel.dev/upload', $file->getClientOriginalName() )) {
                    echo 'uploaded';
                }

The file can be uploaded successsfully though it will create a folder named "laravel.dev" which is my host and inside of it creates another folder named "upload". On the other side, If I set my code like this:

if ($file->move( asset('uploaded'), $file->getClientOriginalName() )) {
                    echo 'uploaded';
                }

It throws this error: Laravel Error

What I wanted to achieve is to upload the file directly to the "upload" folder I created in "projectname/public/" public folder. How can I achieve that?

Upvotes: 0

Views: 1494

Answers (2)

Marwelln
Marwelln

Reputation: 29413

As Juan posted, you are using url paths instead of file paths. Laravel have some path helpers to help you with this problem.

If you would want to select a folder like public/uploaded you can use public_path().'/uploaded. If you would want to selected a folder like app/storage/uploaded you can use storage_path().'/uploaded.

In your example you can use this code:

if ($file->move( public_path().'/uploaded', $file->getClientOriginalName() )) {
    echo 'uploaded';
}

Upvotes: 0

Juan David Decano
Juan David Decano

Reputation: 166

You can use $_SERVER['DOCUMENT_ROOT'] instead.

$var = $_SERVER['DOCUMENT_ROOT'].'/uploads';

if ( $file->move($var, $file->getClientOriginalName()) ) {
    echo 'uploaded';
}

You use path not URLs like laravel.dev/upload.

Upvotes: 1

Related Questions