1919/**
2020 * UploadFileByURL represents the information for an file by url address.
2121 *
22- * You can call [[initWithModel()]] or [[initWithUrl()]] or [[initWithUrlAndModel()]] with to retrieve the instance of file object,
23- * and then use [[saveAs()]] to save it on the server.
22+ * You can call [[initWithModel()]] or [[initWithUrl()]] or [[initWithUrlAndModel()]]
23+ * with to retrieve the instance of file object, and then use [[saveAs()]] to save it on the server.
2424 * You may also query other information about the file, including [[name]],
2525 * [[extension]], [[type]], [[size]] etc.
2626 *
27- *
2827 * @author Skliar Ihor <skliar.ihor@gmail.com>
2928 * @since 1.0
3029 */
3130class UploadFromUrl extends Object
3231{
33- /**
34- * @var string the original name of the file being uploaded
35- */
36- public $ name ;
37- /**
38- * @var string the name of the file without extension being uploaded
39- */
40- public $ baseName ;
32+ /**
33+ * @var string the original name of the file being uploaded
34+ */
35+ public $ name ;
36+ /**
37+ * @var string the name of the file without extension being uploaded
38+ */
39+ public $ baseName ;
4140 /**
4241 * @var string the MIME-type of the uploaded file (such as "image/gif").
4342 * Since this MIME type is not checked on the server side, do not take this value for granted.
@@ -58,10 +57,10 @@ class UploadFromUrl extends Object
5857 */
5958 public $ extension ;
6059
61- public $ url ;
62- public $ model ;
63- public $ attribute ;
64- public $ isWithModel = false ;
60+ public $ url ;
61+ public $ model ;
62+ public $ attribute ;
63+ public $ isWithModel = false ;
6564
6665 /**
6766 * String output.
@@ -74,73 +73,74 @@ public function __toString()
7473 return $ this ->name ;
7574 }
7675
77- public static function initWithUrl ($ url )
76+ public static function initWithUrl ($ url )
7877 {
7978 return self ::createInstance ([
80- 'url ' => $ url
81- ]);
79+ 'url ' => $ url
80+ ]);
8281 }
8382
84- public static function initWithModel ($ model , $ attribute )
85- {
83+ public static function initWithModel ($ model , $ attribute )
84+ {
8685 return self ::createInstance ([
87- 'url ' => $ model ->{$ attribute },
88- 'isWithModel ' => true ,
89- 'model ' => $ model ,
90- 'attribute ' => $ attribute ,
91- ]);
92- }
86+ 'url ' => $ model ->{$ attribute },
87+ 'isWithModel ' => true ,
88+ 'model ' => $ model ,
89+ 'attribute ' => $ attribute ,
90+ ]);
91+ }
9392
94- public static function initWithUrlAndModel ($ url , $ model , $ attribute )
95- {
93+ public static function initWithUrlAndModel ($ url , $ model , $ attribute )
94+ {
9695 return self ::createInstance ([
97- 'url ' => $ url ,
98- 'isWithModel ' => true ,
99- 'model ' => $ model ,
100- 'attribute ' => $ attribute ,
101- ]);
102- }
96+ 'url ' => $ url ,
97+ 'isWithModel ' => true ,
98+ 'model ' => $ model ,
99+ 'attribute ' => $ attribute ,
100+ ]);
101+ }
103102
104103 public static function getInstance ($ model , $ attribute )
105104 {
106105 return self ::initWithModel ($ model , $ attribute );
107106 }
108107
109- public function saveAs ($ file , $ saveToModel = false )
110- {
111- if ($ saveToModel && $ this ->isWithModel ) {
112- $ this ->model ->{$ this ->attribute } = $ file ;
113- } else if ($ this ->isWithModel ) {
114- $ this ->model ->{$ this ->attribute } = null ;
115- }
116-
117- return copy ($ this ->url , $ file );
118- }
119-
120- protected static function createInstance ($ options )
121- {
122- $ options = self ::extendOptions ($ options );
123- return new static ($ options );
124- }
125-
126- protected static function extendOptions (array $ options )
127- {
128- $ parsed_url = parse_url ($ options ['url ' ]);
129- $ headers = @get_headers ($ options ['url ' ], 1 );
130-
131- if (!$ parsed_url || !$ headers || !preg_match ('/^(HTTP)(.*)(200)(.*)/i ' , $ headers [0 ])) {
132- $ options ['error ' ] = UPLOAD_ERR_NO_FILE ;
133- }
108+ public function saveAs ($ file , $ saveToModel = false )
109+ {
110+ if ($ saveToModel && $ this ->isWithModel ) {
111+ $ this ->model ->{$ this ->attribute } = $ file ;
112+ } elseif ($ this ->isWithModel ) {
113+ $ this ->model ->{$ this ->attribute } = null ;
114+ }
134115
135- $ fname = explode ('/ ' , $ parsed_url ['path ' ]);
136- $ options ['name ' ] = end ($ fname );
137- $ options ['baseName ' ] = explode ('. ' , $ options ['name ' ], 1 );
138- $ ext = explode ('. ' , $ options ['name ' ]);
139- $ options ['extension ' ] = mb_strtolower (end ($ ext ));
116+ return copy ($ this ->url , $ file );
117+ }
140118
141- $ options ['size ' ] = isset ($ headers ['Content-Length ' ]) ? $ headers ['Content-Length ' ] : 0 ;
142- $ options ['type ' ] = isset ($ headers ['Content-Type ' ]) ? $ headers ['Content-Type ' ] : FileHelper::getMimeTypeByExtension ($ options ['name ' ]);
119+ protected static function createInstance ($ options )
120+ {
121+ $ options = self ::extendOptions ($ options );
122+ return new static ($ options );
123+ }
143124
144- return $ options ;
145- }
125+ protected static function extendOptions (array $ options )
126+ {
127+ $ parsed_url = parse_url ($ options ['url ' ]);
128+ $ headers = get_headers ($ options ['url ' ], 1 );
129+
130+ if (!$ parsed_url || !$ headers || !preg_match ('/^(HTTP)(.*)(200)(.*)/i ' , $ headers [0 ])) {
131+ $ options ['error ' ] = UPLOAD_ERR_NO_FILE ;
132+ }
133+
134+ $ options ['name ' ] = isset ($ parsed_url ['path ' ]) ? pathinfo ($ parsed_url ['path ' ], PATHINFO_BASENAME ) : '' ;
135+ $ options ['baseName ' ] = isset ($ parsed_url ['path ' ]) ? pathinfo ($ parsed_url ['path ' ], PATHINFO_FILENAME ) : '' ;
136+ $ options ['extension ' ] = isset ($ parsed_url ['path ' ])
137+ ? mb_strtolower (pathinfo ($ parsed_url ['path ' ], PATHINFO_EXTENSION ))
138+ : '' ;
139+ $ options ['size ' ] = isset ($ headers ['Content-Length ' ]) ? $ headers ['Content-Length ' ] : 0 ;
140+ $ options ['type ' ] = isset ($ headers ['Content-Type ' ])
141+ ? $ headers ['Content-Type ' ]
142+ : FileHelper::getMimeTypeByExtension ($ options ['name ' ]);
143+
144+ return $ options ;
145+ }
146146}
0 commit comments