Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

spacedog4's avatar

Shared hosting uploading images

I'm using a shared hosting to my website, so the content of public folder is on www and the project is in a folder named laravel outside of it.

The problem is, everything is working well instead of my image upload, here is where I write the file

file_put_contents(public_path() . '/upload' . $dir . $name_save, $data)

but instead of saving it on www/upload it saves in laravel/public/upload so when I try to call my image mysite.com/upload/image.png it doens't work cause it can't find the image

What am I missing?

0 likes
13 replies
D9705996's avatar

You should just need to add the following to bootstrap/app.php changing the path to match your environment

$app->bind('path.public', function() {
    return realpath(__DIR__.'/../');
});

https://stackoverflow.com/a/51355638

Haven't tried this so YMMV buvthere are other possible solutions in the linked answer

1 like
spacedog4's avatar

@d9705996 I allready have a bind for public path

App::bind('path.public', function () {
    return base_path() . '/public';
});

the problem is that every call is working, asset, etc, but when it ill write on public_path it writes on the other folder

D9705996's avatar

So just as a test if you hardcode your path does everything work?

file_put_contents('www/upload' . $dir . $name_save, $data)
spacedog4's avatar

@D9705996 - It gives me an file_put_contents(www/upload/projeto/capa-c838d46888f9c95d82ee7963c3aff5685bec70143fc62.png): failed to open stream: No such file or directory I think it tries to find a www folder inside the laravel folder, and can't

jlrdw's avatar

First read https://laravel.com/docs/5.7/requests#storing-uploaded-files

Next store the uploaded image, and just the file name in the database.

Next use the asset helper:

asset()

The asset function generates a URL for an asset using the current scheme of the request (HTTP or HTTPS):

$url = asset('img/photo.jpg');

Make sure you have in htaccess

    RewriteEngine On
    RewriteBase /laravel57/  # use your site here

And read these post:

http://novate.co.uk/deploy-laravel-5-on-shared-hosting-from-heart-internet/

https://laracasts.com/discuss/channels/tips/hosting-on-shared-server-best-practice

Asset helper usage:

<img src="<?php echo asset('assets/upload/imgdogs') . '/' . $row->dogpic; ?>" alt="" class="image">

Just that easy.

I don't use blade, so convert as needed.

There's no way this doesn't work, I have had several sites on shared, with zero (0) problems.

1 like
skimuli's avatar

Most shared hosts enable users to create symbolic links... please map `www/upload' -> 'laravel/public/upload'

Think this will help

1 like
spacedog4's avatar

@JLRDW - But my upload is from a base64, not a file, cause the user can crop the image before upload, and when I use asset it calls the wrong folder, it calls the folder from www instead of the laravel

And Where do I place the code for htaccess? in laravel, laravel/public or www?

Snapey's avatar

when you said you already have bind for public path, it sayspublic when it should be saying www

you also need to make it relative to the laravel folder, so, if laravel folder is at the same level as www

App::bind('path.public', function () {
    return base_path() . '../www';
});

test it in tinker, it will be quicker than keep uploading images

>>> public_path(). it should show the full file system path to your www folder

1 like
jlrdw's avatar

I have worked with images quite a bit a good package could do this for you and you won't have to store the contents of the image, just the file name.

Storing the images in a large field takes up a lot of room.

1 like
spacedog4's avatar

@snapey my public_path should return towww folder? it shouldn't return the laravel/public folder?

spacedog4's avatar

@JLRDW - I don't store the image in the database, the user crops the image than it sends the base64 in the request and I convert it to a file, save on a folder and store the filename in the database

spacedog4's avatar

@SNAPEY - The problem is, my public_path aims to the laravel/public folder on root, and the asset generate an URL and this URL aims to www so the problem is when I hit http://engenhariamrc.com.br/upload it should be hitting laravel/public/upload but it hits www/upload

spacedog4's avatar

@SNAPEY - It worked, yaaay, but instead of ../www I used /../www, but now, every time I update a file in the public folder I should update the public folder of outside the project (~/www) and the other files in the normal folder (~/laravel) is it right?

Please or to participate in this conversation.