I try to upload a file use psr 7 in slim and its showing this error


#1

Hi there , I am using slim to create my back-end rest api and itry to upload a file use psr 7 in slim and its showing this error upload target path is not writable. please help how to fix it … Thanks in Advance.

                //image upload ..
                $avatar = $request->getUploadedFiles()['avatar'];
                if (empty($avatar)) {
                  return $response->withHeader('Content-Type' , 'application/json')
                            ->withJson([
                              'code' => '404',
                              'message' => 'Expected a avatar..']);
                }

                if($avatar->getError() === UPLOAD_ERR_OK){
                  $uploadFileName = $avatar->getClientFilename();
                  $base_path = __DIR__ . "../assets/uploads/images/";


                  // var_dump($base_path); die();
                  $avatar->moveTo($base_path);

#2

Hi “sure”, can you see what permissions you have on the folder __DIR__ ../assets/uploads/images/ is?


#3

Bro this is my folder permission drwxrwxrwx 2 username username 4096 May 10 13:39


#4

I fixed this error @martisj :slight_smile:
The look like this like

$avatar = $request->getUploadedFiles()[‘avatar’];

                $upload_path = __DIR__ . "/../uploads/images/";

                $uploadFileName = $avatar->getClientFilename();

                $full_img_path = $upload_path.$uploadFileName ;

                if(empty($avatar)){
                    return $response->withHeader('Content-Type', 'application/json')
                                    ->withJson([
                                      'code' => '404',
                                      'message' => 'Expected a avatar'
                                    ]);
                }

                if($avatar->getError() === UPLOAD_ERR_OK) $avatar->moveTo($full_img_path);

#5

Thank you so mach @martisj


#6

Help me help you. Good job Sure.