@@ -566,4 +566,76 @@ public static function getUserAgent(): ?string
566566 {
567567 return Headers::get ('HTTP_USER_AGENT ' );
568568 }
569+
570+ /**
571+ * Store a file from the request.
572+ *
573+ * @param string $key The name of the file input the request.
574+ * @param string $destination The directory where the file should be stored.
575+ * @param array $configs Optional configurations: max_file_size, file_type, extensions
576+ * @return array An array containing the status, path, and error message.
577+ */
578+ public static function store (string $ key , string $ destination , array $ configs = []): object
579+ {
580+ $ configs ["unique " ] = true ;
581+
582+ # See PR notes #1
583+ if (isset ($ configs ["extensions " ])) {
584+ $ file = self ::get ($ key );
585+ $ fileExtension = pathinfo ($ file ["name " ], PATHINFO_EXTENSION );
586+ if (!in_array ($ fileExtension , $ configs ["extensions " ])) {
587+ return (object ) [
588+ 'status ' => false ,
589+ 'error ' => 'Invalid file extension. '
590+ ];
591+ }
592+ }
593+
594+ $ fileSystem = new \Leaf \FS ;
595+ $ uploadedFile = $ fileSystem ::uploadFile (self ::get ($ key ), $ destination , $ configs );
596+ if (!$ uploadedFile )
597+ return (object ) [
598+ 'status ' => false ,
599+ 'error ' => $ fileSystem ::$ errorsArray ['upload ' ]
600+ ];
601+
602+ return (object ) array_shift ($ fileSystem ::$ uploadInfo );
603+ }
604+
605+ /**
606+ * Store a file from the request with a specific name.
607+ *
608+ * @param string $key The name of the file input the request.
609+ * @param string $destination The directory where the file should be stored.
610+ * @param string $filename The name to give the stored file.
611+ * @param array $configs Optional configurations: max_file_size, file_type, extensions
612+ * @return array An array containing the status, path, and error message.
613+ */
614+ public static function storeAs (string $ key , string $ destination , string $ filename , array $ configs = []): object
615+ {
616+ $ configs ["rename " ] = true ;
617+ $ configs ["name " ] = $ filename ;
618+
619+ # See PR notes #1
620+ if (isset ($ configs ["extensions " ])) {
621+ $ file = self ::get ($ key );
622+ $ fileExtension = pathinfo ($ file ["name " ], PATHINFO_EXTENSION );
623+ if (!in_array ($ fileExtension , $ configs ["extensions " ])) {
624+ return (object ) [
625+ 'status ' => false ,
626+ 'error ' => 'Invalid file extension. '
627+ ];
628+ }
629+ }
630+
631+ $ fileSystem = new \Leaf \FS ;
632+ $ uploadedFile = $ fileSystem ::uploadFile (self ::get ($ key ), $ destination , $ configs );
633+ if (!$ uploadedFile )
634+ return (object ) [
635+ 'status ' => false ,
636+ 'error ' => $ fileSystem ::$ errorsArray ['upload ' ]
637+ ];
638+
639+ return (object ) array_shift ($ fileSystem ::$ uploadInfo );
640+ }
569641}
0 commit comments