Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for uploading files (of any type) #852

Open
imadBelaroussi opened this issue Feb 9, 2022 · 3 comments
Open

API for uploading files (of any type) #852

imadBelaroussi opened this issue Feb 9, 2022 · 3 comments
Assignees

Comments

@imadBelaroussi
Copy link

imadBelaroussi commented Feb 9, 2022

I want to share my custom controller to add the api to upload files of any type.

you can use this API http://localhost:8080/api.php/upload with the POST method to send your files, with the variable file(configurable with the "$name_in_files" variable), you will find your files in the directory archive/ (configurable with the "$path" variable).

if you are motivated, then go copy this code and you will have this best API for this wonderful project

namespace ImadDev\CustomController {
    use Psr\Http\Message\ResponseInterface;
    use Psr\Http\Message\ServerRequestInterface;
    use Tqdev\PhpCrudApi\Cache\Cache;
    use Tqdev\PhpCrudApi\Column\ReflectionService;
    use Tqdev\PhpCrudApi\Controller\Responder;
    use Tqdev\PhpCrudApi\Database\GenericDB;
    use Tqdev\PhpCrudApi\Middleware\Router\Router;

    class MyCustomController {
    
        private $responder;
    
        public function __construct(Router $router, Responder $responder, GenericDB $db, ReflectionService $reflection, Cache $cache)
        {
            $router->register('POST', '/upload', array($this, 'getUploadFile'));
            $this->responder = $responder;
        }
    
         public function getUploadFile(ServerRequestInterface $request): ResponseInterface
        {
            $path = "archive/";
            $name_in_files = 'file';

            if(!isset($_FILES[$name_in_files])) return $this->responder->error(1003, $name_in_files,['message' => "filename should be 'file'"]);
            elseif($_FILES[$name_in_files]['error']!=0) return $this->responder->error(1008, $name_in_files,['message' => "error saving file"]);
            elseif(file_exists($path.$_FILES[$name_in_files]['name'])) return $this->responder->error(1009, '',['message' => "filename duplicated : ".$_FILES[$name_in_files]['name']]);
            else{
                if(!is_dir($path)) mkdir($path);

                $name = $_FILES[$name_in_files]['name'];

                move_uploaded_file($_FILES[$name_in_files]['tmp_name'],$path.$name);
                
                return $this->responder->success(['message' => "file saved successfully"]);
            }
        }
    }
}

and add this in config parameter:

$config = new Config([
                ...
                'customControllers' => 'ImadDev\CustomController\MyCustomController',
                ...
]);

the variable $path is to put the folder where the files will be stored (by default "archive/")
and the variable $name_in_files is the name of the variable in you sent with your files (by default "file")

@mevdschee mevdschee self-assigned this Feb 9, 2022
@drascom
Copy link

drascom commented Apr 19, 2023

i got 404 not found error

@scriptPilot
Copy link

I have 404 as well - somewhere in this block:

if(!isset($_FILES[$name_in_files])) return $this->responder->error(1003, $name_in_files,['message' => "filename should be 'file'"]);
          elseif($_FILES[$name_in_files]['error']!=0) return $this->responder->error(1008, $name_in_files,['message' => "error saving file"]);          
          elseif(file_exists($path.$_FILES[$name_in_files]['name'])) return $this->responder->error(1009, '',['message' => "filename duplicated : ".$_FILES[$name_in_files]['name']]);
          
          else{
              if(!is_dir($path)) mkdir($path);
              $name = $_FILES[$name_in_files]['name'];

              move_uploaded_file($_FILES[$name_in_files]['tmp_name'],$path.$name);
              return $this->responder->success(['message' => "file saved successfully"]);
          }

When I uncomment, there is no more error 404.
Success messages are properly given to frontend.

Error messages are shown always as 404 or 500.

@scriptPilot
Copy link

scriptPilot commented Oct 16, 2023

Basically, already this line results in error 404 return:

if(!isset($_FILES[$name_in_files])) return $this->responder->error(1003, $name_in_files,['message' => "filename should be 'file'"]);

This line results in a proper response:

return $this->responder->success(['message' => "file saved successfully"]);

Does anybody have a clue or solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants