Laravel 5 & # 8594; getRealPath () doenst show the correct value

In my local development, I use the code shown below, which works fine,

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

What does not exist on my hosting. But I found where the temporary images are stored on my hosting, which is located at tmp/ located here: http://gyazo.com/e0199718324119109a4ff66321414e12 .

How to change value ->getRealPath() 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 use the Image/intervention package to upload and store images.

+6
source share
2 answers

Most likely your root is your account /data/sites/web/christophvhbe , and that getRealPath is completely accurate (what you see on FTP is probably chrooted ), As a result, your $imagePath is actually a problem.

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

Or, even better, use the Laravel base_path and / or public_path , so if you ever change hosting, it continues to work if the path changes:

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

If you refer to your error logs, I bet you will find permission errors by trying to create a file in the /images server directory, which you can definitely not create or access on shared hosting.

+3
source

Just use it

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

Source: https://habr.com/ru/post/989491/


All Articles