laravel 5 ->getRealPath() doenst show correct value

那年仲夏 提交于 2019-12-01 16:32:51

问题


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.


回答1:


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.




回答2:


Simply use this

$_SERVER['DOCUMENT_ROOT']."/path/to/directory"


来源:https://stackoverflow.com/questions/30970532/laravel-5-getrealpath-doenst-show-correct-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!