laravel 5 ->getRealPath() doenst show correct value

Christophvh picture Christophvh · Jun 22, 2015 · Viewed 18.1k times · Source

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.

Answer

ceejayoz picture ceejayoz · Jun 22, 2015

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.