Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pol_php's avatar

problem with creating folder on debian os

Hello I have next problem after I import my laravel app from WIN 10 to Debian 8.3. In my app when some create ofert in db he can upload scan file to server. Path to store folder is 'myProcject/public/storage/doc/$year/$month', simple. After I install app on debian evrything is work until someone wants to upload file to server. I got error mkdir(): Permission denied. I have tried change privilages to all folder to 777 and etc. but nothing work still got this error. Has anyone already encountered such a problem?

0 likes
12 replies
markus.heb's avatar

Have you checked if the directory belongs to the right user & group?

You can check it like this

cd path-to-your-laravel-project
ls -la

If there is something other like www-data or nginx than that is probably the error.

You can fix the ownership permissions like that

sudo chown -R www-data:www-data path-to-your-laravel-project

or if you use nginx

sudo chown -R nginx-nginx path-to-your-laravel-project

This should fix most permission related error.

And avoid using 777 ;) 644 is should be more than enough.

slev1n's avatar

Try to change owner of while storage directory for web-server user

// get web-server user (apache for example)
ps -elf | grep apache

// get found user group (www-data for example)
group www-data

// apache (default www-data)
chown www-data:www-data storage -R

I think it will be enough for all web-server + php manipulation on that directory.

Be sure that your code manipulating correct folder, use standard laravel Storage facade instead of raw php.

If not helps - copy/paste here your exception message.

pol_php's avatar

Thank you all for replay. I had check and right now privilages looks like this

  1. when I'm in var/www and make ls -la I get

drwxr-xr-x | www-data | www-data | 4096 jun 4 10:24 myProject

  1. when I'm in var/www/myProject and make ls -la I get

drwvrwxrwx | www-data | www-data | 4096 jun 4 10:24 public

So when it comes to privileges, is it enough ?

My exception message is

ErrorException (E_WARNING) mkdir(): Permission denied

markus.heb's avatar

It would be interessting to see if the owner of "storage" and "storage/docs" is correct too.

pol_php's avatar

Funny thing. Every were (storage and storage/doc) is drwvrwxrwx | www-data | www-data | 4096 jun 4 10:24. For now I'm trying create folder like this

mkdir($myPath, 0777, true);

So I changed to

File::makeDirectory($myPath), 0777, true;

Right now it create folder not in public/storage but in /public. If someone corrects me, I just change the wayto create a folder and it will maybe help ?

markus.heb's avatar

I think the "recomended" laravel way is this:

Storage::makeDirectory($directory);

There is no need to specify the rights 777. If you need this to get it going, there is usually something other wrong.

To get the right path you can use the storage_path helper

$directory=storage_path("doc/{$year}/{$month}")
pol_php's avatar

Ok now creating path correct but not upload with method storeAs. No errors. Can it be somehow related?

markus.heb's avatar

Can you please provide an example Code, this would help a lot ;)

pol_php's avatar

Shure. I believe that this fragment should suffice

if($request->hasFile('doc_scan'))
        {
            $file = $request->file('doc_scan');
            $fName= $file->getClientOriginalName();

            $path = Scans::setPath($year[0],$year[1],'doc');

            if(!file_exists($path))
            {
                //mkdir($path, 0777, true);
                File::makeDirectory(public_path('storage/').$path, 0777, true);
            }

            $check = $path.$fName;

          if(Storage::exists($check) || Scans::existSkan($check))
          {
              return back()->with('error','file exist!');
          }
          else
          {
              if ($file->storeAs($path, $fName))
              {
                  $scan = new Scans();
                  $scan->path = $path . $fName;
                  $scan->doc_id= $paper->doc_id;
                  $scan->save();
              }
          }
        }

I check he should add data to db if it upload file on server but it not upload and still insert data to db.

markus.heb's avatar

I think, I found the problem.

you are using public_path('storage') for the path.

The storeAs method is using the local disk as default which points points to storage_path('app') (as configured in 'config/filesystem.php'. When you pass the $path variable to the storeAs method, these two get concatinated together and der result is something like

storage_path('app').'/'.$path

you have two possibilities, either you can do this

public_path('storage/app/').$path
// or that
storage_path('app').'/'.$path

to generate the path for the makeDirectory() method.

As an addition, I am not sure if the trailing slash in the *_path() methods are working correctly.

I think the better approach would be to do something like this

public_path('storage/app/'.$path)
// or
storage_path('app/'.$path);
1 like
slev1n's avatar

You can make your own 'disk' and forget about wrong path, path_helpers and other stuff!

// add config/filesystem.php 
'disks' =>
    ...
    'scans' => [
            'driver' => 'local',
            'root'  => public_path('doc')
        ],

// controller(or service or whatever else).php
if($request->hasFile('doc_scan') {

    $file = $request->file('doc_scan');
    $name = $file->getClientOriginalName();
    $path = "{$year[0]}/{$year[1]}/";

    $storage = Storage::disk('scans');

    if($storage->exists($path.$name) || Scans::exists($path.$name))
        return back()->with('error', 'File exists');

    if($storage->putFileAs($path, $file, $name))
        $scan = Scan::create([
            'path' => $path.name,
            'doc_id' => $paper->doc_id
        ]);
}

putFileAs() will create folders if they missing.

pol_php's avatar

Ok now it works. Thank you very much for yours time @slev-ua and @markus.heb. I would give you both best answer but im not shure can I do that however, I am still very grateful for yours help

Please or to participate in this conversation.