You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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")
The text was updated successfully, but these errors were encountered:
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 variablefile
(configurable with the "$name_in_files
" variable), you will find your files in the directoryarchive/
(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
and add this in config parameter:
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")
The text was updated successfully, but these errors were encountered: