Inquisitive's avatar

mkdir(): permission denied for laravel chunk upload

my dropzone script

Dropzone.autoDiscover = false;
    let myDropzone = new Dropzone(".dropzone", {
    autoProcessQueue: false,
    uploadMultiple: false,
    parallelUploads: 10,
    maxFiles: 10,
    addRemoveLinks: true,
    maxFilesize: 15000,
    chunking: true,
    chunkSize: 2000000,
});

// Append token to the request - required for web routes
myDropzone.on('sending', function (file, xhr, formData) {

    let context_item_id = CONTEXT_ITEM_ID;
    let selectedTreeItem = getSelectedTreeItem();
    let parent_id = '0';
    if(context_item_id){
        formData.append('parent_id', context_item_id);
    }else if(selectedTreeItem){
        parent_id = selectedTreeItem.mime_type === 'folder' ? selectedTreeItem.id : selectedTreeItem.parent;
        formData.append('parent_id', parent_id);
    }

})

myDropzone.on("error", function(file, errorMessage) {
    console.error("File failed to upload:", file.name);

    // Handle the error, e.g., display a notification to the user
    alertifyError("Failed to upload " + file.name + ". Error: " + errorMessage);
});

myDropzone.on("queuecomplete", function(file, res) {
    updateTreeData();
    myDropzone.removeAllFiles(true);
});

and on the controller side this is my code:

public function upload(Request $request)
{
    $requestData = $request->all();

    try{
        // create the file receiver
        $receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request));

        // check if the upload is success, throw exception or return response you need
        if ($receiver->isUploaded() === false) {
            throw new UploadMissingFileException();
        }

        // receive the file
        $save = $receiver->receive();

        // check if the upload has finished (in chunk mode it will send smaller files)
        if ($save->isFinished()) {
            // save the file and return any response you need, current example uses `move` function. If you are
            // not using move, you need to manually delete the file by unlink($save->getFile()->getPathname())
            return $this->saveFile($requestData, $save->getFile());
        }

        // we are in chunk mode, lets send the current progress
        /** @var AbstractHandler $handler */
        $handler = $save->handler();

        return response()->json([
            "done" => $handler->getPercentageDone(),
            'status' => true
        ]);
    }catch(\Exception $e){
        return response(config('app.env') != 'production' ? $e->getMessage() : 'Something Went Wrong', 500);
    }

}

public function saveFile($requestData, HttpUploadedFile $file)
{
    $user_group = auth()->user()->user_group;
    $parent_id = $requestData['parent_id'] ?? '0';
    $parent_path = $this->documentService->getParentPath($parent_id);
    $data = $this->assetStorageService->uploadFileToAwsStorage($file,$parent_path, 'private');
    $file_data = $this->documentService->prepareDataForSelectedFile($data['path']);
    UploadedDocument::create([
        'user_group_id' => $user_group,
        'created_by'    => auth()->user()->id,
        'name'          => $data['doc_name'],
        'mime_type'     => $file_data['mime_type'],
        'file_type'     => $file_data['type'],
        'extension'     => $file_data['extension'],
        'path'          => $data['path'],
        'parent_id'     => $parent_id == "0" ? '0' : $parent_id,
        'visibility'    => DocumentVisibilityConstant::PRIVATE,
        'file_size'     => $file_data['file_size'],
    ]);

    return response([
        'success' => true,
        'message' => "Successfully Uploaded",
        'done'    => 100,
        'fileName'=> $data['doc_name']
    ],200);
}

and the service function uploadFileToAwsStorage

    public function uploadFileToAwsStorage($fileObject, $attachmentPath, $visibility = 'public'): array
    {
        $docName = $fileObject->getClientOriginalName();
        $docExtension = $fileObject->getClientOriginalExtension();

        $path = Storage::disk('s3')->put($attachmentPath, $fileObject, $visibility);

        return [
            'doc_name' => $docName,
            'doc_extension' => $docExtension,
            'path' => $path,
            'file_size' => $fileObject->getClientSize()
        ];
    }

Error I am getting:

#message: "mkdir(): Permission denied"
  #code: 0
  #file: "/var/www/project/vendor/pion/laravel-chunk-upload/src/Save/ChunkSave.php"
  #line: 267
  #severity: E_WARNING

I have already tried by creating a folder storage/app/chunks and have already provided the permission. I have also tried with these commands.

sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache
chmod -R 775 storage
chmod -R 775 bootstrap/cache

But still getting the same issue when ever i try to upload large files (its because only it tries to use this chunks folder when it needs to chunk the large file.)

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error message "mkdir(): Permission denied" indicates that the web server does not have the necessary permissions to create the "chunks" folder in the storage directory. To resolve this issue, you can try the following steps:

  1. Make sure that the "chunks" folder exists in the storage directory. If it doesn't exist, create it manually.

  2. Set the correct ownership and permissions for the storage directory and its contents. Run the following commands in your project's root directory:

sudo chown -R www-data:www-data storage
sudo chmod -R 775 storage
  1. If you are using Laravel's default file system configuration, make sure that the local disk is configured correctly in your config/filesystems.php file. It should look like this:
'disks' => [
    // ...
    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],
    // ...
],
  1. If you are using a different file system configuration, make sure that the disk you are using has the correct permissions and configuration.

  2. Restart your web server to apply the changes.

If the issue persists, you may need to check your server's file system permissions and ensure that the web server user has the necessary permissions to create directories and write files in the storage directory.

Please or to participate in this conversation.