php-crud-api icon indicating copy to clipboard operation
php-crud-api copied to clipboard

API for uploading files (of any type)

Open imadBelaroussi opened this issue 3 years ago • 3 comments

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")

imadBelaroussi avatar Feb 09 '22 08:02 imadBelaroussi

i got 404 not found error

drascom avatar Apr 19 '23 00:04 drascom

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 avatar Oct 16 '23 17:10 scriptPilot

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?

scriptPilot avatar Oct 16 '23 17:10 scriptPilot