laravel 5 ->getRealPath() doenst show correct value
Asked Answered
M

2

7

On my local development I use the code shown below, which works perfect,

but when I uploaded the site to my shared hosting everything worked fine except my file upload. I already determined that the problem is involving ->getRealPath(), when I dd(); that I get this path: /data/sites/web/christophvhbe/tmp

Which doesn't exist on my hosting. But I found where the temporary images are stored on my hosting, which is in the tmp/ located here: http://gyazo.com/e0199718324119109a4ff66321414e12.

How can I change the ->getRealPath() value to the correct value?

$fileName = time() . '-' . $request->file('foto')->getClientOriginalName();
$product->foto = $fileName;
$product->save();

$imagePath = '/images/producten/'. $fileName;

$image = Image::make($request->file('foto')->getRealPath())->fit(300)->save($imagePath);

I'm using the Image/intervention package to upload and store images.

Marysa answered 22/6, 2015 at 0:3 Comment(0)
R
4

Chances are your account's root is /data/sites/web/christophvhbe, and that getRealPath is entirely accurate (what you see in FTP is likely chrooted). As a result, your $imagePath is actually the problem.

$imagePath = '/data/sites/web/christophvhbe/images/producten/'. $fileName;

Or, better yet, use Laravel's base_path and/or public_path helpers so if you ever change hosting it keeps working if the paths change:

$imagePath = public_path() . '/images/producten/' . $fileName;

If you consult your error logs, I'll bet you find permissions errors trying to create a file in the server's /images directory, which on shared hosting you definitely won't be able to create or access.

Rudder answered 22/6, 2015 at 2:2 Comment(4)
what you are suggesting is the actual problem, public_path() and getRealPath are not accurate at all. the actual path to the image folder is : www.christophvh.be/images/producten/img.png . But that is not what it's saying, it is trying to acces a temporary map. which image/intervention uses to temporary store the original image i think. I know where the correct temporary map is, but i don't know how to access it, because it's higher up than my www folder. shown in the link i posted on topMarysa
So if i use your method i get this error: NotWritableException in Image.php line 138: Can't write image data to path (/data/sites/web/christophvhbe/laravel/public/images/producten/1434939947-thosiba.png)Marysa
Path and URL are totally different things and you should understand the difference. Using my method, you have the correct path. You're now getting a different issue, which is that the webserver can't write to the laravel/public/images directory. You can adjust its permissions to 777 with chmod, or if your host allows use ACLs and/or chown you can give the webserver access that way. Consult your host if you need help with that - what you can do varies from host to host. You should also consider getting off shared hosting if you're going to run Laravel.Rudder
@Chris If you took the 777 approach, please note that it's fairly unsafe on shared hosting - the other users on the server can write to that directory. Consult your host for help or get on a VPS (DigitalOcean is $5/month, for example) so you can be more secure.Rudder
M
0

Simply use this

$_SERVER['DOCUMENT_ROOT']."/path/to/directory"
Moretta answered 27/6, 2019 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.