@@ -206,6 +206,21 @@ private function __initNewClient($config = array())
206
206
}
207
207
}
208
208
209
+ //filePathCompatible
210
+ $ this ->filePathCompatible = false ;
211
+ if (version_compare (phpversion (), '7.0.0 ' , '< ' )) {
212
+ if (OssUtil::isWin ()) {
213
+ $ this ->filePathCompatible = true ;
214
+ }
215
+ }
216
+ if (isset ($ config ['filePathCompatible ' ])) {
217
+ if ($ config ['filePathCompatible ' ] === true ) {
218
+ $ this ->filePathCompatible = true ;
219
+ } else if ($ config ['filePathCompatible ' ] === false ) {
220
+ $ this ->filePathCompatible = false ;
221
+ }
222
+ }
223
+
209
224
self ::checkEnv ();
210
225
}
211
226
@@ -1867,7 +1882,7 @@ public function uploadFile($bucket, $object, $file, $options = NULL)
1867
1882
{
1868
1883
$ this ->precheckCommon ($ bucket , $ object , $ options );
1869
1884
OssUtil::throwOssExceptionWithMessageIfEmpty ($ file , "file path is invalid " );
1870
- $ file = OssUtil:: encodePath ($ file );
1885
+ $ file = $ this -> encodeFilePath ($ file );
1871
1886
if (!file_exists ($ file )) {
1872
1887
throw new OssException ($ file . " file does not exist " );
1873
1888
}
@@ -1985,7 +2000,7 @@ public function appendFile($bucket, $object, $file, $position, $options = NULL)
1985
2000
$ this ->precheckCommon ($ bucket , $ object , $ options );
1986
2001
1987
2002
OssUtil::throwOssExceptionWithMessageIfEmpty ($ file , "file path is invalid " );
1988
- $ file = OssUtil:: encodePath ($ file );
2003
+ $ file = $ this -> encodeFilePath ($ file );
1989
2004
if (!file_exists ($ file )) {
1990
2005
throw new OssException ($ file . " file does not exist " );
1991
2006
}
@@ -2651,7 +2666,7 @@ public function multiuploadFile($bucket, $object, $file, $options = null)
2651
2666
if (empty ($ file )) {
2652
2667
throw new OssException ("parameter invalid, file is empty " );
2653
2668
}
2654
- $ uploadFile = OssUtil:: encodePath ($ file );
2669
+ $ uploadFile = $ this -> encodeFilePath ($ file );
2655
2670
if (!isset ($ options [self ::OSS_CONTENT_TYPE ])) {
2656
2671
$ options [self ::OSS_CONTENT_TYPE ] = $ this ->getMimeType ($ object , $ uploadFile );
2657
2672
}
@@ -2750,7 +2765,7 @@ public function uploadDir($bucket, $prefix, $localDirectory, $exclude = '.|..|.s
2750
2765
if (!is_string ($ prefix )) throw new OssException ("parameter error, prefix is not string " );
2751
2766
if (empty ($ localDirectory )) throw new OssException ("parameter error, localDirectory is empty " );
2752
2767
$ directory = $ localDirectory ;
2753
- $ directory = OssUtil:: encodePath ($ directory );
2768
+ $ directory = $ this -> encodeFilePath ($ directory );
2754
2769
//If it's not the local directory, throw OSSException.
2755
2770
if (!is_dir ($ directory )) {
2756
2771
throw new OssException ('parameter error: ' . $ directory . ' is not a directory, please check it ' );
@@ -2768,7 +2783,9 @@ public function uploadDir($bucket, $prefix, $localDirectory, $exclude = '.|..|.s
2768
2783
self ::OSS_PART_SIZE => self ::OSS_MIN_PART_SIZE ,
2769
2784
self ::OSS_CHECK_MD5 => $ checkMd5 ,
2770
2785
);
2771
- $ realObject = (!empty ($ prefix ) ? $ prefix . '/ ' : '' ) . $ item ['file ' ];
2786
+ //mbstring to utf-8
2787
+ $ fileName = $ this ->decodeFilePath ($ item ['file ' ]);
2788
+ $ realObject = (!empty ($ prefix ) ? $ prefix . '/ ' : '' ) . $ fileName ;
2772
2789
2773
2790
try {
2774
2791
$ this ->multiuploadFile ($ bucket , $ realObject , $ item ['path ' ], $ options );
@@ -3485,6 +3502,69 @@ private function getRegion()
3485
3502
return $ this ->region ;
3486
3503
}
3487
3504
3505
+ /**
3506
+ * Encodes the file path from UTF-8 to GBK.
3507
+ *
3508
+ * @param $filepath
3509
+ * @return string
3510
+ */
3511
+ private function encodeFilePath ($ filepath )
3512
+ {
3513
+ if ($ this ->filePathCompatible !== true ) {
3514
+ return $ filepath ;
3515
+ }
3516
+
3517
+ if (empty ($ filepath )) {
3518
+ return $ filepath ;
3519
+ }
3520
+
3521
+ try {
3522
+ $ encoding = array ('UTF-8 ' ,'GB2312 ' , 'GBK ' );
3523
+ $ encode = mb_detect_encoding ($ filepath , $ encoding );
3524
+ if ($ encode !== 'UTF-8 ' ) {
3525
+ return $ filepath ;
3526
+ }
3527
+ $ tmp = iconv ($ encode , 'GBK ' , $ filepath );
3528
+ if ($ tmp !== false ) {
3529
+ $ filepath = $ tmp ;
3530
+ }
3531
+ } catch (\Exception $ e ) {
3532
+ //IGNORE
3533
+ }
3534
+ return $ filepath ;
3535
+ }
3536
+
3537
+ /**
3538
+ * Decodes the file path from GBK to UTF-8.
3539
+ *
3540
+ * @param $filepath
3541
+ * @return string
3542
+ */
3543
+ private function decodeFilePath ($ filepath )
3544
+ {
3545
+ if ($ this ->filePathCompatible !== true ) {
3546
+ return $ filepath ;
3547
+ }
3548
+ if (empty ($ filepath )) {
3549
+ return $ filepath ;
3550
+ }
3551
+
3552
+ try {
3553
+ $ encoding = array ('UTF-8 ' ,'GB2312 ' , 'GBK ' );
3554
+ $ encode = mb_detect_encoding ($ filepath , $ encoding );
3555
+ if ($ encode === 'UTF-8 ' || $ encode === false ) {
3556
+ return $ filepath ;
3557
+ }
3558
+ $ tmp = iconv ($ encode , 'UTF-8 ' , $ filepath );
3559
+ if ($ tmp !== false ) {
3560
+ $ filepath = $ tmp ;
3561
+ }
3562
+ } catch (\Exception $ e ) {
3563
+ //IGNORE
3564
+ }
3565
+ return $ filepath ;
3566
+ }
3567
+
3488
3568
/**
3489
3569
* Check if all dependent extensions are installed correctly.
3490
3570
* For now only "curl" is needed.
@@ -3702,4 +3782,6 @@ public function setConnectTimeout($connectTimeout)
3702
3782
private $ signer ;
3703
3783
3704
3784
private $ checkObjectEncoding = false ;
3785
+
3786
+ private $ filePathCompatible ;
3705
3787
}
0 commit comments