在我的本地开发中,我使用下面显示的代码,它工作得很好,
但是当我将网站上传到我的共享主机时,除了我的文件上传之外,一切正常。我已经确定问题涉及 ->getRealPath()
,当我dd();
我得到这条路时: /data/sites/web/christophvhbe/tmp
我的主机上不存在。但是我找到了临时图像在我的主机上的存储位置,该主机位于此处的tmp/
中:http://gyazo.com/e0199718324119109a4ff66321414e12。
如何将->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);
我正在使用Image/intervention
包上传和存储图像。
您
帐户的根目录很可能是/data/sites/web/christophvhbe
,并且该getRealPath
是完全准确的(您在FTP中看到的内容可能是chroot的)。因此,您的$imagePath
实际上是问题所在。
$imagePath = '/data/sites/web/christophvhbe/images/producten/'. $fileName;
或者,更好的是,使用 Laravel 的base_path
和/或public_path
助手,因此如果您更改托管,如果路径更改,它会继续工作:
$imagePath = public_path() . '/images/producten/' . $fileName;
如果您查阅错误日志,我敢打赌,您会发现尝试在服务器的/images
目录中创建文件的权限错误,在共享主机上,您绝对无法创建或访问该文件。
只需使用这个
$_SERVER['DOCUMENT_ROOT']."/path/to/directory"