laravel 5 ->getRealPath() 没有显示正确的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30970532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:48:58  来源:igfitidea点击:

laravel 5 ->getRealPath() doenst show correct value

phplaravellaravel-5intervention

提问by Christophvh

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

但是当我将网站上传到我的共享主机时,除了文件上传之外,一切正常。我已经确定问题涉及到 ->getRealPath(),当dd();我得到这条路径时: /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.

我的主机上不存在。但我发现临时图像存储在我的主机上,tmp/位于此处:http: //gyazo.com/e0199718324119109a4ff66321414e12

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

如何将->getRealPath()值更改为正确的值?

$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/interventionpackage to upload and store images.

我正在使用该Image/intervention包上传和存储图像。

采纳答案by ceejayoz

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

很有可能您的帐户的 root 是/data/sites/web/christophvhbe,这getRealPath是完全准确的(您在 FTP 中看到的可能是chrooted)。结果,您$imagePath实际上是问题所在。

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

Or, better yet, use Laravel's base_pathand/or public_pathhelpers so if you ever change hosting it keeps working if the paths change:

或者,更好的是,使用 Laravelbase_path和/或public_path帮助程序,因此如果您更改托管方式,它会在路径更改时继续工作:

$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 /imagesdirectory, which on shared hosting you definitely won't be able to create or access.

如果您查阅错误日志,我敢打赌您会发现尝试在服务器/images目录中创建文件的权限错误,而在共享主机上,您肯定无法创建或访问该文件。

回答by Thomas Chirwa

Simply use this

简单地使用这个

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