ollie_123's avatar

HEELP.. Live Server Not Able To Download Files

Hi All

I need some help asap. I've put my site onto a live server and when i login to the website, i cant access files that users have uploaded such as avatars & documents etc.

On the live server the folder structure is as follows:

/home	
/laravel_project //this has all of the controllers, models etc
/Domains/mydomain/index.php  //and all other public files etc

when i upload a file it goes to laravel_project/admin/avatars/

but when i try to download the file from the website it can see it as i can see the filename when i hover over download button but says no file found. If i move the avatar or file to /Domains/mydomain/admin/avatars i can download it.

Please can someone advise.

Thank you in advance.

0 likes
21 replies
drewdan's avatar

Have you run the php artisan storage:link command on your production server?

ollie_123's avatar

Hey @drewdan

Thank you for the prompt response much appreciated.

I did indeed, it says the links have been created but i still cant download the file. Is there something i'm missing? This is the first time deploying to a live server so not had this issue before.

Thanks in advance.

drewdan's avatar

Are you uploading the files through Laravel, or manually via FTP?

I think, though I am not 100% sure as files on servers always cause me grief, that when you upload a file, if your default driver is set to local, they may not be downloadable.

Have you set a default disk driver in your config? If it is set to local, it may not work correctly in production. It should be set to public if you are going to be downloading the files.

https://laravel.com/docs/7.x/filesystem#file-visibility is a good place to look to check out the file visibility stuff

ollie_123's avatar

Hey @drewdan

I'm currently uploading them though my Laravel App and attempting to download them through the Laravel App.

I think the problem is that its uploading them to home/laravel_project/admin/avatars/ when it should be uploading them to home/domains/mydomain/admin/avatars

It can read the former but not download. I've set permissions to 777 but still no joy.

If its any help, in my controller i'm uploading them as such

        $image->move(public_path('admin/avatars'), $new_name);

and to download them i'm using:

<a download href="{{ URL::to('/') }}/admin/avatars/{{ $users->avatar_name }}">Original File</a>
drewdan's avatar
$file = $request->file('avatar')->store('admin/avatars', 'public');

Maybe try uploading the file like this? I am guessing you are passing it in from a form request?

And then you can access it using the storage facade:

$url = Storage::url($file);

That should then return a URL with the file.

See if that works for you

ollie_123's avatar

Hey @drewdan

Thanks for the suggestion. I am indeed uploading avatars & files via a form request and storing the file name in the database for retrieval purposes.

A big one is customers files. This is where the customer uploads a file, admin amends it and uploads the revised file so that the user can download it. Both files have their name stored in the orders table.

So it's now uploading to Storage/App/Public/Avatars but i'm confused about the $url = Storage::url($file);. Is that meant to go inside of the Href? Also how does it know which file to pull?

Could i not just store files in the public folder under the domain instead?

Snapey's avatar

Lets establish a baseline. When you load index.php does it respond at mywebsite.com/index.php or does it need other folders mentioned in the url?

ollie_123's avatar

Hey @snapey

The index.php folder is in the public folder on the server for that domain which is: home/domains/mydomain/

When i go to the domain in chrome it loads as www.domain.co.uk.

drewdan's avatar

My thought was:

$file = $request->file('avatar')->store('admin/avatars', 'public'); //this does not need to be public, just needs to be the correct filesystem for your setup
//the file variable should contain a hashed file name for this avatar store this in the database
$avatar = Avatar::create(['file' => $file]);

//you can then retrieve the file name from the database however you do
$avatar = Avatar::first(); //just using this as an example

$url = Storage::url($avatar->file); //you pass it the hash of the file which is how it finds the file in the file system
//this just generates a url to that file which you can put in a href if that works for you
//if you want to force the file to download
return Storage::download($avatar->file);
//but this would need to be done in a controller (I think)

@snapey is better than me at this, so he may have some more insight - or possibly explain it better :D

Snapey's avatar

ok, so if you store the file in storage/app/public/avatars then when accessed in the browser, the file should be seen in the folder /storage/avatars

So, first check, when you save the file, does it appear in the file system in the correct folder.

next. Knowing the filename, can you load it in the browser (via the address bar) at mysite.com/avatars/...

ollie_123's avatar

Hi @snapey

I tried accessing it via the URL but got a 403 error.

I think the issue is thats its storing the file in the incorrect folder.

On my live server i've got

/root
/domains/mydomain/public/admin/... //This is where i want the files to be stored
/laravel_project/public/admin/... //This is where the files are currently being stored.

Going back a couple of steps, originally on my local server i was using:-

$image = $request->file('dealer_file');
        $new_name = $image->getClientOriginalName();
        $image->move(public_path('admin/tuningfiles'), $new_name);

and was retrieving the files in the table in the view from the database like this:

<td data-title="Gearbox"><a download href="{{ URL::to('/') }}/admin/tuningfiles/{{ $pending->dealer_file }}">Original File</a></td>
Snapey's avatar

if you just copied the files from development, you need to run php artisan config:clear

ollie_123's avatar

Hey @snapey

Thank you. I just tried it but with no joy. It's still uploading to the main folder (laravel_project) in the root of the server and not the public folder (Domains/mydomain/)

On the post i read about going live, it mentioned keeping everything except the public folder in the root & then the public folder contents go into the public folder on the server.

Im baffled on this and due to go live tomorrow morning. :(

Snapey's avatar

You have mentioned so many paths I am confused

  • /home
  • /laravel_project
  • /Domains/mydomain/index.php
  • laravel_project/admin/avatars/
  • Domains/mydomain/admin/avatars
  • home/laravel_project/admin/avatars/
  • home/domains/mydomain/admin/avatars
  • public_path('admin/avatars'
  • /admin/avatars/{{ $users->avatar_name }}
  • Storage/App/Public/Avatars
  • home/domains/mydomain/
  • /root
  • /domains/mydomain/public/admin/...
  • /laravel_project/public/admin/...
  • public_path('admin/tuningfiles')
  • {{ URL::to('/') }}/admin/tuningfiles/
  • Domains/mydomain/
ollie_123's avatar

Sorry, @snapey

So the paths on the live server are as follows

/---home
/---domains/mydomain/ 	//This folder contains index.php, admin/tuningfiles, css, js
/---laravel_project/	 //This folder contains app, bootstrap, config, database, node_modules, public/admin/tuningfiles (this is where the files are currently going to on upload)

In my file upload controller i have:-

$image = $request->file('dealer_file');
        $new_name = $image->getClientOriginalName();
        $image->move(public_path('admin/tuningfiles'), $new_name);

and in my view, to download the file i've been using

<a download href="{{ URL::to('/') }}/admin/tuningfiles/{{ $pending->dealer_file }}">Original File</a>

Thank you for your help on this.

Snapey's avatar
Snapey
Best Answer
Level 122

All your project files should be in the parent of the public folder, not in a separate, parallel folder.

I guess you have edited index.php to point to your project code?

ollie_123's avatar

Hey @snapey

Ah ok thats interesting. Ok so i should move laravel_project into

/---home
/---domains
	---/laravel_project/
	---/mydomain/

Yeah i edited index.php to point to project code but i can amend that easily enough.

After moving that, how can i tell my download link to look at the laravel_project folder. Is it just a case of amending the anchor tag to

<a download href="{{ URL::to('/') }}/../admin/tuningfiles/{{ $pending->dealer_file }}">Original File</a>
Snapey's avatar

no, you cannot access the files above your domain's public folder

This is what the symlink is for. To link the public/storage folder to the storage/app/public folder.

What folder does your web server serve as document root? Whatever that folder is, your app should be in that folder - not a folder called laravel-project

ollie_123's avatar

Hey @snapey

I see, but my public folder (in mydomain) doesn't have a storage folder? I guess it will create this as required.

So i've moved the laravel_project into the parent folder of /mydomain

how do i now retrieve the file from the storage folder inside the laravel_project/public with the symlink?

Thank you in advance.

ollie_123's avatar

Thanks Snapey. I think tiredness had set bad in last night as i should have followed your instruction earlier. By moving the file to the parent folder and pointing the domain at the public folder, the symlink created perfectly.

Thank you to both @drewdan & @snapey for your time and help. I really appreciate it.

1 like

Please or to participate in this conversation.