Lost
Lost

Reputation: 109

PHP fopen(): Filename cannot be empty cause by getRealPath return false

I'm developing Laravel project on Windows 10 locally using Laragon

PHP version: 7.1.8 64bit NTS

related php.ini that I know is

post_max_size = 8M

file_uploads = On


Source code:

// if no image uploaded
if (!$request->hasFile('profile_picture')) 
throw new \Exception("No image found");

// get uploaded image
$image = $request->file('profile_picture');

// store to storage/app/users/
Storage::putFileAs('users', $image ,auth()->id());

enter image description here

In my opinion, this is server configuration issue, probably problem on php.ini,

but I'm not familiar with server configuration, and there is not so many topics online related with this issue.

I know the problem cause, but I don't know how to solve it.

Upvotes: 2

Views: 5816

Answers (2)

Lost
Lost

Reputation: 109

After 2 days, I finally figured out why. Last time, I used PHP-TS (Thread Safe) version to develop this application and it worked normally.

By now, I'm using PHP-NTS (Non Thread Safe) version and, after testing, I was able to confirm that this issue was caused by different file info result of NTS & TS

For PHP 7.1.7 64bit TS TS

For PHP 7.1.8 64bit NTS NTS

So by now we can know that bug occurs on temp folder setting,

For NTS version, temp folder on C:\Windows\Temp,

No permission of this folder cause PHP can't read the realPath (I guess), realPath return false

For TS version, temp folder on C:\Users\YQuan\AppData\Local\Temp,

Allow to access, realPath readable

Solution

  1. Change permission to folder effected

    I won't using this method because too messy to change permission on Windows system

  2. set upload_tmp_dir in php.ini to "C:\Users\{username}\AppData\Local\Temp", then restart server

    this method is easier.

Upvotes: 3

SiNONiMiTY
SiNONiMiTY

Reputation: 705

Are you sure that the user that runs your Web Server Process User has RW (Read/Write) access to Windows TEMP Directory? If no, you will definitely fail!

A possible fix for this is to set the PHP TEMP Dir to a readable/writeable location.

On your php.ini set this directive.

upload_tmp_dir = 'PATH'

Make sure that the 'PATH' exists and your Web Server Process User has read/write access to it.

Web Server Process User is the account that is used by your web server to run the service. You can check this on the Task Manager.

Upvotes: 5

Related Questions