Angel Miladinov
Angel Miladinov

Reputation: 1655

Laravel 5.4 fopen(): Filename cannot be empty

I want to upload image in laravel 5.4 Here's the code:

if ($request->hasFile('image') && $request->image->isValid()) {
        $image = $request->image;

        $image_name = bcrypt($image->getClientOriginalName());

        Storage::disk('public')->putFileAs('images', $image, $image_name . '.' . $image->getClientOriginalExtension());

        $article->image($image_name);
    }

in FilesystemAdapter.php (line 146)

I checked the FileSystemAdapter.php and saw that the problem is in this line:

$stream = fopen($file->getRealPath(), 'r+');

When I var dump the $file variable it returns the correct info, but when I var_dump($file->getRealPath(); it says bool(false) and I can't upload the image

Upvotes: 13

Views: 18951

Answers (3)

Yevgeniy Afanasyev
Yevgeniy Afanasyev

Reputation: 41320

We had this problem with Internet Information Services (IIS) for Windows® Server. And only thing needed was - permission for C:\Windows\temp because it is set as upload_tmp_dir in php.ini for php7.

Upvotes: 26

saber tabatabaee yazdi
saber tabatabaee yazdi

Reputation: 4959

My case was:

The target folder did not exist there. And I created it. So the error is gone.

laravel admin drive is defined here:

Config/admin.php

You can define it in public folder, for example : upload folder in public.

My target folder (images) should be created in /uploads/ not in public

So I create images in upload and error is gone

Upvotes: 0

vinsa
vinsa

Reputation: 1242

My case with Laragon 4.0.12 on Windows 10.

The problem was that upload_tmp_dir=... in my php.ini had not set needed permissions.

If this setting is commented the value should be the default windows temp directory (C:\Windows\Temp you can check your permissions for it).

The solution - I changed it to:

upload_tmp_dir = "C:\Users\{my_user}\AppData\Local\Temp" 

in order to avoid messing up with changing permissions.

Restart the server after the change to take effect.

Another settings in php.ini to consider:

upload_max_filesize = 
max_file_uploads = 

Also following php extensions are needed to be enabled: exif, gd2

Upvotes: 12

Related Questions