1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
csv_data.php
См. документацию.
1<?php
8
10{
11 var $sFileName; // full file name
12 var $sContent; // file contents
13 var $iFileLength; // file length
14 var $iCurPos = 0; // current file position Fetch
15 var $cFieldsType = "R"; // fields type: R - with delimiter, F - fixed width
16 var $cDelimiter = ";"; // field delimiter
17 var $arWidthMap = array(); // array of delimiters positions in fixed width case
18 var $bFirstHeader = false; // 1 row - columns titles
19
20 var $__file = null;
21 var $__buffer = "";
24 var $__hasBOM = false;
25
26 public function __construct($fields_type = "R", $first_header = false)
27 {
28 $this->SetFieldsType($fields_type);
29 $this->SetFirstHeader($first_header);
30 }
31
32 public function LoadFile($filename)
33 {
34 $this->sFileName = $filename;
35 $file = fopen($this->sFileName, "rb");
36 if (is_resource($file))
37 {
38 $this->__file = $file;
39 $this->iFileLength = filesize($this->sFileName);
40 $this->CheckUTF8BOM();
41 $this->SetPos();
42 return true;
43 }
44 return false;
45 }
46
47 public function CloseFile()
48 {
49 if($this->__file)
50 {
51 fclose($this->__file);
52 $this->__file = null;
53 }
54 }
55
56 public function CheckUTF8BOM()
57 {
58 if (!$this->__file)
59 {
60 return;
61 }
62
63 //check UTF-8 Byte-Order Mark
64 fseek($this->__file, 0);
65 $sBOM = fread($this->__file, 3);
66 if($sBOM == "\xEF\xBB\xBF")
67 {
68 $this->__hasBOM = true;
69 }
70 }
71
72 public function SetFieldsType($fields_type = "R")
73 {
74 $this->cFieldsType = ($fields_type == "F"? "F" : "R");
75 }
76
77 public function SetDelimiter($delimiter = ";")
78 {
79 $this->cDelimiter = (mb_strlen($delimiter) > 1? mb_substr($delimiter, 0, 1) : $delimiter);
80 }
81
82 public function SetFirstHeader($first_header = false)
83 {
84 $this->bFirstHeader = $first_header;
85 }
86
87 public function GetFirstHeader()
88 {
90 }
91
92 public function SetWidthMap($arMap)
93 {
94 $this->arWidthMap = array();
95 for ($i = 0, $n = count($arMap); $i < $n; $i++)
96 {
97 $this->arWidthMap[$i] = intval($arMap[$i]);
98 }
99 }
100
101 public function FetchDelimiter()
102 {
103 if (!$this->__file)
104 {
105 return false;
106 }
107
108 $bInString = false;
109 $str = "";
110 $res_r = array();
111 while ($this->iCurPos < $this->iFileLength)
112 {
113 $ch = $this->__buffer[$this->__buffer_pos];
114 if ($ch == "\r" || $ch == "\n")
115 {
116 if (!$bInString)
117 {
118 while ($this->iCurPos < $this->iFileLength)
119 {
120 $this->IncCurPos();
121 $ch = $this->__buffer[$this->__buffer_pos];
122 if ($ch != "\r" && $ch != "\n")
123 {
124 break;
125 }
126 }
127 if ($this->bFirstHeader)
128 {
129 $this->bFirstHeader = false;
130 $res_r = array();
131 $str = "";
132 continue;
133 }
134 else
135 {
136 $res_r[] = $str;
137 return $res_r;
138 }
139 }
140 }
141 elseif ($ch == "\"")
142 {
143 if (!$bInString)
144 {
145 $bInString = true;
146 $this->IncCurPos();
147 continue;
148 }
149 else
150 {
151 $this->IncCurPos();
152 if($this->__buffer[$this->__buffer_pos]!="\"")
153 {
154 $bInString = false;
155 continue;
156 }
157 }
158 }
159 elseif ($ch == $this->cDelimiter)
160 {
161 if (!$bInString)
162 {
163 $res_r[] = $str;
164 $str = "";
165 $this->IncCurPos();
166 continue;
167 }
168 }
169
170 //inline "call"
171 $this->iCurPos++;
172 $this->__buffer_pos++;
173 if($this->__buffer_pos >= $this->__buffer_size)
174 {
175 if(feof($this->__file))
176 {
177 $this->__buffer = "";
178 }
179 else
180 {
181 $this->__buffer = fread($this->__file, 1024*1024);
182 }
183 $this->__buffer_size = strlen($this->__buffer);
184 $this->__buffer_pos = 0;
185 }
186
187 $str .= $ch;
188 }
189
190 if ($str <> '')
191 {
192 $res_r[] = $str;
193 }
194
195 if(empty($res_r))
196 {
197 return false;
198 }
199 else
200 {
201 return $res_r;
202 }
203 }
204
205 public function FetchWidth()
206 {
207 if (!$this->__file)
208 {
209 return false;
210 }
211
212 $str = "";
213 $ind = 1;
214 $jnd = 0;
215 $res_r = array();
216
217 while ($this->iCurPos < $this->iFileLength)
218 {
219 $ch = $this->__buffer[$this->__buffer_pos];
220 if ($ch == "\r" || $ch == "\n")
221 {
222 while ($this->iCurPos < $this->iFileLength)
223 {
224 $this->IncCurPos();
225 $ch = $this->__buffer[$this->__buffer_pos];
226 if ($ch != "\r" && $ch != "\n")
227 {
228 break;
229 }
230 }
231 if ($this->bFirstHeader)
232 {
233 $this->bFirstHeader = false;
234 $res_r = array();
235 $ind = 1;
236 $str = "";
237 continue;
238 }
239 else
240 {
241 $res_r[] = $str;
242 return $res_r;
243 }
244 }
245 elseif ($ind == $this->arWidthMap[$jnd])
246 {
247 $res_r[] = $str.$ch;
248 $str = "";
249 $this->IncCurPos();
250 $ind++;
251 $jnd++;
252 continue;
253 }
254
255 //inline "call"
256 $this->iCurPos++;
257 $this->__buffer_pos++;
258 if($this->__buffer_pos >= $this->__buffer_size)
259 {
260 if(feof($this->__file))
261 {
262 $this->__buffer = "";
263 }
264 else
265 {
266 $this->__buffer = fread($this->__file, 1024*1024);
267 }
268 $this->__buffer_size = strlen($this->__buffer);
269 $this->__buffer_pos = 0;
270 }
271
272 $ind++;
273 $str .= $ch;
274 }
275
276 if ($str <> '')
277 {
278 $res_r[] = $str;
279 }
280
281 if(empty($res_r))
282 {
283 return false;
284 }
285 else
286 {
287 return $res_r;
288 }
289 }
290
291 public function Fetch()
292 {
293 if ($this->cFieldsType == "R")
294 {
295 if ($this->cDelimiter == '')
296 {
297 return false;
298 }
299 return $this->FetchDelimiter();
300 }
301 else
302 {
303 if (empty($this->arWidthMap))
304 {
305 return false;
306 }
307 return $this->FetchWidth();
308 }
309 }
310
311 public function IncCurPos()
312 {
313 if (!$this->__file)
314 {
315 return;
316 }
317
318 $this->iCurPos++;
319 $this->__buffer_pos++;
320 if($this->__buffer_pos >= $this->__buffer_size)
321 {
322 if(feof($this->__file))
323 {
324 $this->__buffer = "";
325 }
326 else
327 {
328 $this->__buffer = fread($this->__file, 1024*1024);
329 }
330 $this->__buffer_size = strlen($this->__buffer);
331 $this->__buffer_pos = 0;
332 }
333 }
334
335 public function MoveFirst()
336 {
337 $this->SetPos();
338 }
339
340 public function GetPos()
341 {
342 return $this->iCurPos;
343 }
344
345 public function SetPos($iCurPos = 0)
346 {
347 if (!$this->__file)
348 {
349 return;
350 }
351
352 $iCurPos = intval($iCurPos);
353 if ($iCurPos <= $this->iFileLength)
354 {
355 $this->iCurPos = $iCurPos;
356 }
357 else
358 {
359 $this->iCurPos = $this->iFileLength;
360 }
361
362 $pos = $this->iCurPos;
363 if($this->__hasBOM)
364 {
365 $pos += 3;
366 }
367 fseek($this->__file, $pos);
368
369 if(feof($this->__file))
370 {
371 $this->__buffer = "";
372 }
373 else
374 {
375 $this->__buffer = fread($this->__file, 1024*1024);
376 }
377 $this->__buffer_size = strlen($this->__buffer);
378 $this->__buffer_pos = 0;
379 }
380
381 public function SaveFile($filename, $arFields)
382 {
383 $this->sFileName = $filename;
384
385 if ($this->cFieldsType == 'R' && $this->cDelimiter <> '')
386 {
387 $this->sContent = '';
388 for ($i = 0, $n = count($arFields); $i < $n; $i++)
389 {
390 if ($i>0)
391 {
392 $this->sContent .= $this->cDelimiter;
393 }
394 $pos1 = mb_strpos($arFields[$i], $this->cDelimiter);
395 $pos2 = mb_strpos($arFields[$i], "\"");
396 $pos3 = mb_strpos($arFields[$i], "\n");
397 $pos4 = mb_strpos($arFields[$i], "\r");
398 if ($pos1 !== false || $pos2 !== false || $pos3 !== false || $pos4 !== false)
399 {
400 $this->sContent .= "\"";
401 $this->sContent .= str_replace("\"", "\"\"", $arFields[$i]);
402 $this->sContent .= "\"";
403 }
404 else
405 {
406 $this->sContent .= $arFields[$i];
407 }
408 }
409 if ($this->sContent <> '')
410 {
411 $this->sContent .= "\n";
412 $file_id = fopen($this->sFileName, "ab");
413 fwrite($file_id, $this->sContent);
414 fclose($file_id);
415 }
416 }
417 }
418}
Определения csv_data.php:10
GetPos()
Определения csv_data.php:340
IncCurPos()
Определения csv_data.php:311
$iCurPos
Определения csv_data.php:14
$__hasBOM
Определения csv_data.php:24
$sFileName
Определения csv_data.php:11
$iFileLength
Определения csv_data.php:13
$__buffer_size
Определения csv_data.php:23
SetWidthMap($arMap)
Определения csv_data.php:92
$__buffer_pos
Определения csv_data.php:22
$sContent
Определения csv_data.php:12
SetFieldsType($fields_type="R")
Определения csv_data.php:72
$arWidthMap
Определения csv_data.php:17
Fetch()
Определения csv_data.php:291
$__file
Определения csv_data.php:20
CloseFile()
Определения csv_data.php:47
SaveFile($filename, $arFields)
Определения csv_data.php:381
LoadFile($filename)
Определения csv_data.php:32
$__buffer
Определения csv_data.php:21
$cFieldsType
Определения csv_data.php:15
FetchWidth()
Определения csv_data.php:205
FetchDelimiter()
Определения csv_data.php:101
SetPos($iCurPos=0)
Определения csv_data.php:345
GetFirstHeader()
Определения csv_data.php:87
CheckUTF8BOM()
Определения csv_data.php:56
$cDelimiter
Определения csv_data.php:16
SetFirstHeader($first_header=false)
Определения csv_data.php:82
$bFirstHeader
Определения csv_data.php:18
MoveFirst()
Определения csv_data.php:335
__construct($fields_type="R", $first_header=false)
Определения csv_data.php:26
SetDelimiter($delimiter=";")
Определения csv_data.php:77
$str
Определения commerceml2.php:63
$arFields
Определения dblapprove.php:5
$filename
Определения file_edit.php:47
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
else $ch
Определения group_list_element_edit.php:27
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$n
Определения update_log.php:107