English 中文(简体)
确保 fgetcsv () 读取整行
原标题:Ensure fgetcsv() reads the entire line
  • 时间:2012-05-24 22:11:23
  •  标签:
  • php
  • csv

我正使用 PHP 从 CSV 文件导入数据, 使用 fgetcsv (), 它生成每行的数组 。 最初, 我将字符限制设置在 1024 上, 类似 :

while ($data = fgetcsv($fp, 1024)) {
  // do stuff with the row
}

然而,200多列的CSV超过多行的1024限制,这导致读取的行停在行中,然后下一个fgetcsv () 的呼叫将开始于上一个关闭的位置,等等,直到达到 EOL 。

从那时起,我已将这一限制提高到4096年,这应该照顾大多数案件,但我想放张支票,确保每行被接通后,整行都读完。

I was thinking to check the end of the last element of the array for end of line characters ( , , ), but wouldn t these be parsed out by the fgetcsv() call?

最佳回答

感谢您的建议, 但是这些解决方案真的没有解答 : 在仍然提供限制的同时, 我们是否计算了最长的线条 。 我通过 < code> wc - L UNIX 命令, 通过 < code>shell_ exec > () 来确定在开始获取线条之前的文件的最长线 。 代码如下 :

// open the CSV file to read lines
$fp = fopen($sListFullPath,  r );

// use wc to figure out the longest line in the file
$longestArray = explode(" ", shell_exec( wc -L   . $sListFullPath));
$longest_line = (int)$longestArray[0] + 4; // add a little padding for EOL chars

// check against a user-defined maximum length
if ($longest_line > $line_length_max) {
    // alert user that the length of at least one line in the CSV is too long
}

// read in the data
while ($data = fgetcsv($fp, $longest_line)) {
    // do stuff with the row
}

这种办法确保每行全部读完,并且仍然为真正长的行提供一个安全网,而不逐行通过整个文件,用PHP线逐行。

问题回答

省略长度参数。 在 PHP5 中, 它是可选参数 。

while ($data = fgetcsv($fp)) {
  // do stuff with the row
}

只需不指定限制, Fgetcsv () 就会在完整行所必要的范围内流出。 如果您确实指定了限制, 那么完全由您来扫描文件流, 确保您不会在中间切除某个东西 。

然而, 请注意, 不指定一个限制, 如果首先无法控制此. csv 的生成, 则风险会很大 。 很容易用恶意的 CSV 来淹没服务器, 而 CSV 在单行上拥有许多兆字节的数据 。

我会小心处理您的最终解决方案 。 我上传了一个名为 < code> /.; ls - a;. csv 的文件来执行命令注射。 如果您使用此方法, 请确认文件路径 。 另外, 如果您的 < code> default_ later 因任何原因失败, 提供 < code> < wc 可能是一个好主意 。

// use wc to find max line length
// uses a hardcoded default if wc fails
// this is relatively safe from command 
// injection since the file path is a tmp file
$wc = explode(" ", shell_exec( wc -L   . $validated_file_path));
$longest_line = (int)$wc[0];
$length = ($longest_line) ? $longest_line + 4 : $default_length;

fgetcsv() is by default is used to read line by line from a csv file but when it is not functioning that way, you have to check PHP_EOL character on your OS machine you have simply to go: C:xamppphpphp.ini and search for:

; auto_ contect_line_ends = 关闭

并卸载它,并激活它到:

auto_detect_line_ends = 在

重新启动 Apache 并检查.... 是否有效





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签