lakamark's avatar

Directory Generation problem

HI, everyone!

I have a problem with my uploader system.In my website, I have a general uploader. Users can upload files on any content kind on my web site. (Posts, Project, Video, etc).

When Laravel generate my directory. It generate wrong directory. Like this :

uploads.project.1/my_file.jpg

There are only folder this name : uploads.project.1. In this case, I should have 3 folders generated by my uploader system.

My test expected to have this result :

uploads/project/1/my_file.jpg.

You guessed, my tests were failure. : This test always was failure :

public function testDeleteParentContentDeleteAllAttachments()
    {
        $response = $this->callController();
        $attachment = $response->json();
        factory(Attachment::class, 3)->create();
        $this->assertFileExists($this->getFileForAttachment($attachment));
        $this->assertEquals(4, Attachment::count());
        Project::first()->delete();
        $this->assertFileNotExists($this->getFileForAttachment($attachment));
        $this->assertEquals(3, Attachment::count());
    }

Here the function to upload files in a directory

/**
     * Upload a file
     * @param string $destination
     * @param UploadedFile $file
     * @return $this
     */
    public function uploadFile(string $destination ,UploadedFile $file)
    {
        $path = $this->generateFolders($destination);
        $file = $file->storePublicly($path, ['disk' => 'public']);
        $this->name = basename($file);
        return $this;
    }

Where I called my function UploadFile() :

public function store (AttachmentRequest $request) {
        $type = $request->get('attachable_type');
        $id = $request->get('attachable_id');
        $file = $request->file('image');
        if (class_exists($type) && method_exists($type, 'attachments')) {
            $subject = call_user_func($type . '::find', $id);
            if ($subject) {
                $stringType = explode('\', $type);
                $f = lcfirst($stringType[1]);
                $attachment = new Attachment($request->only('attachable_type', 'attachable_id', 'user_id'));
                $attachment->uploadFile("uploads/{$f}/{$id}" , $file);
                $attachment->save();
                return $attachment;
            } else {
                return new JsonResponse(
                    ['attachable_id' => "You can't upload files on a fake content"],
                    422
                );
            }
        } else {
            return new JsonResponse(['attachable_type' => "This"], 422);
        }
    }

I tried the native php function mkdir() and also the Laravel facade File with the function makeDirectory(). I had the same result my tests were failure, but there are an exception : ErrorException mkdir(): File exits();

Have a good day and tank you for your help

0 likes
0 replies

Please or to participate in this conversation.