@@ -468,6 +468,15 @@ public static function type($filePath)
468468 */
469469 public static function upload ($ file , string $ destination , array $ options = [])
470470 {
471+ $ bucketName = null ;
472+ $ destinationIsBucket = false ;
473+
474+ if (preg_match ('/^([a-zA-Z0-9-_]+):\/\// ' , $ destination , $ matches )) {
475+ $ destinationIsBucket = true ;
476+ $ bucketName = $ matches [1 ];
477+ $ destination = str_replace ($ matches [0 ], '' , $ destination );
478+ }
479+
471480 $ defaultUploadOptions = [
472481 'name ' => null ,
473482 'maxSize ' => 0 ,
@@ -478,56 +487,62 @@ public static function upload($file, string $destination, array $options = [])
478487
479488 $ options = array_merge (static ::$ fileCreateOptions , $ defaultUploadOptions , $ options );
480489
481- $ destinationPath = new Path ($ destination );
482- $ destination = $ destinationPath ->normalize ();
483-
484- if (!Directory::exists ($ destination )) {
485- mkdir ($ destination , $ options ['mode ' ], true );
486- }
487-
488- $ temp = $ file ['tmp_name ' ];
489- $ name = $ options ['name ' ] ?? $ file ['name ' ];
490-
491- if ($ options ['maxSize ' ] > 0 && ($ file ['size ' ] > $ options ['maxSize ' ])) {
492- static ::$ errorsArray ['upload ' ] = 'File size exceeds maximum size ' ;
493- return false ;
494- }
490+ if (!is_resource ($ file )) {
491+ $ temp = $ file ['tmp_name ' ];
492+ $ name = $ options ['name ' ] ?? $ file ['name ' ];
495493
496- if (File::exists ($ destination . DIRECTORY_SEPARATOR . $ name )) {
497- if ($ options ['overwrite ' ]) {
498- unlink ($ destination . DIRECTORY_SEPARATOR . $ name );
499- } else if ($ options ['rename ' ]) {
500- $ name = time () . '_ ' . uniqid () . '_ ' . $ name ;
501- } else {
502- static ::$ errorsArray ['upload ' ] = "$ name already exists " ;
494+ if ($ options ['maxSize ' ] > 0 && ($ file ['size ' ] > $ options ['maxSize ' ])) {
495+ static ::$ errorsArray ['upload ' ] = 'File size exceeds maximum size ' ;
503496 return false ;
504497 }
505- }
506498
507- if ($ options ['validate ' ]) {
508- $ fileType = static ::type ($ temp );
509- $ fileExtension = (new Path ($ file ['name ' ]))->extension (); // Changed from $temp to $file['name'] to fix extension validation
499+ if (File::exists ($ destination . DIRECTORY_SEPARATOR . $ name )) {
500+ if ($ options ['overwrite ' ]) {
501+ unlink ($ destination . DIRECTORY_SEPARATOR . $ name );
502+ } else if ($ options ['rename ' ]) {
503+ $ name = time () . '_ ' . uniqid () . '_ ' . $ name ;
504+ } else {
505+ static ::$ errorsArray ['upload ' ] = "$ name already exists " ;
506+ return false ;
507+ }
508+ }
510509
511- if (
512- !empty ($ options ['allowedTypes ' ]) &&
513- !in_array ($ fileType , $ options ['allowedTypes ' ])
514- ) {
515- static ::$ errorsArray ['upload ' ] = "File should be of type: $ fileType " ;
516- return false ;
510+ if ($ options ['validate ' ]) {
511+ $ fileType = static ::type ($ temp );
512+ $ fileExtension = (new Path ($ file ['name ' ]))->extension (); // Changed from $temp to $file['name'] to fix extension validation
513+
514+ if (
515+ !empty ($ options ['allowedTypes ' ]) &&
516+ !in_array ($ fileType , $ options ['allowedTypes ' ])
517+ ) {
518+ static ::$ errorsArray ['upload ' ] = "File should be of type: $ fileType " ;
519+ return false ;
520+ }
521+
522+ if (
523+ !empty ($ options ['allowedExtensions ' ]) &&
524+ !in_array ($ fileExtension , $ options ['allowedExtensions ' ])
525+ ) {
526+ static ::$ errorsArray ['upload ' ] = 'File extension not allowed ' ;
527+ return false ;
528+ }
517529 }
518530
519- if (
520- !empty ($ options ['allowedExtensions ' ]) &&
521- !in_array ($ fileExtension , $ options ['allowedExtensions ' ])
522- ) {
523- static ::$ errorsArray ['upload ' ] = 'File extension not allowed ' ;
524- return false ;
531+ if (!$ destinationIsBucket ) {
532+ $ destinationPath = new Path ($ destination );
533+ $ destination = $ destinationPath ->normalize ();
534+
535+ if (!Directory::exists ($ destination )) {
536+ mkdir ($ destination , $ options ['mode ' ], true );
537+ }
538+ } else {
539+ $ file = fopen ($ temp , 'r+ ' );
525540 }
526541 }
527542
528543 $ uploadInfo = [
529544 'name ' => $ name ,
530- 'size ' => $ file ['size ' ],
545+ 'size ' => $ file ['size ' ] ?? null ,
531546 'type ' => static ::type ($ name ),
532547 'path ' => (new Path ($ destination . DIRECTORY_SEPARATOR . $ name ))->normalize (),
533548 'extension ' => (new Path ($ name ))->extension (),
@@ -538,6 +553,24 @@ public static function upload($file, string $destination, array $options = [])
538553 )))
539554 ];
540555
556+ if ($ destinationIsBucket ) {
557+ $ result = Bucket::connection ($ bucketName )->upload ($ file , $ destination , [
558+ 'name ' => $ name ,
559+ 'overwrite ' => $ options ['overwrite ' ],
560+ 'rename ' => $ options ['rename ' ],
561+ 'visibility ' => $ options ['visibility ' ] ?? 'public ' ,
562+ ]);
563+
564+ if (!$ result ) {
565+ static ::$ errorsArray ['upload ' ] = Bucket::errors ();
566+ return false ;
567+ }
568+
569+ $ uploadInfo ['url ' ] = (is_string ($ result )) ? $ result : false ;
570+
571+ return $ uploadInfo ;
572+ }
573+
541574 try {
542575 if (move_uploaded_file ($ temp , $ destination . DIRECTORY_SEPARATOR . $ name )) {
543576 return $ uploadInfo ;
@@ -591,6 +624,26 @@ public static function lastModified($filePath)
591624 return filemtime ($ filePath );
592625 }
593626
627+ /**
628+ * Create a resource from a file
629+ *
630+ * @param string $filePath The path of the file to create a resource from
631+ * @param string $mode The mode to open the file in
632+ * @return resource|bool
633+ */
634+ public static function toResource ($ filePath , $ mode = 'r ' )
635+ {
636+ $ path = new Path ($ filePath );
637+ $ filePath = $ path ->normalize ();
638+
639+ if (!static ::exists ($ filePath )) {
640+ static ::$ errorsArray ['file ' ] = 'File does not exist ' ;
641+ return false ;
642+ }
643+
644+ return fopen ($ filePath , $ mode );
645+ }
646+
594647 /**
595648 * Return all errors that occured during file operations
596649 * @return array
0 commit comments