2015-09-27 350 views
2

在我的wamp服务器中,我有一个每秒更新csv文件的脚本。 我想从CSV文件中删除行,我有这样的代码(从这里:How To Delete The Top 100 Rows From a CSV File With PHP):从更新csv文件中删除行

$input = explode("\n", file_get_contents("file.csv")); 
foreach ($input as $line) { 
// process all lines. 
} 

// This function removes first 100 elements. 
// More info: 
// http://php.net/manual/en/function.array-slice.php 
$output = array_slice($input, 100); 
file_put_contents("out.csv", implode("\n", $output)); 

的问题发生了,当我试图运行上面的代码,我得到:

“未能打开流权限被拒绝“

问题是因为文件一直在更新。 我如何知道? 我mada的csv的副本(该文件的内容没有更新),它的成功。

为什么我需要这个?

因为文件越来越大,我必须删除他的行..如果我不这样做,我会得到:“PHP致命错误:允许的内存大小********字节用尽(试图在第54行的/var/www/*******.php中分配71个字节“,即使我写入ini_set(”memory_limit“,”-1“);

我提醒你 - 这是wamp服务器!

有什么想法?

谢谢!

+0

为什么你第一次写信给csv? – PeeHaa

+0

一些重要的数据。因为该文件正在增长,我必须删除他的线..如果我不这样做,我会得到:“PHP致命错误:允许的内存大小********字节用尽(试图分配71个字节)在第578行的/var/www/*******.php中,“即使我写入ini_set(”memory_limit“,”-1“); – jhon4

+0

你不能编辑更新文件的脚本并删除那些额外的行吗? –

回答

1

这不是你问的。但我认为你现有的方案是有缺陷的。然后你正在寻找一个愚蠢的解决方案来解决它。你有多任务问题,这不是一个快速的过程。每当你这样做时,你也可能会失去几行。

要做到这一点的正确方法是有一个文件夹说日志

然后把一个文件名为YYYYMMDD.CSV。每天更改名称。并删除任何说,超过7天。

0
$input = explode("\n", file_get_contents("file.csv")); 
foreach ($input as $line) { 
// process all lines. 
} 

// This function removes first 100 elements. 
// More info: 
// http://php.net/manual/en/function.array-slice.php 
$output = array_slice($input, 100); 
file_put_contents("out.csv", implode("\n", $output)); 

The problem is happened when I trying to run the above code and I get:

"failed to open stream permission denied"

这里有3种情况,你不能修改文件。

  1. 文件已被使用(被另一个进程锁定)。
  2. 文件没有正确的权限(R/W)。或者文件不属于你正在使用的用户/组,必须在php的情况下(www-data)。
  3. 文件不存在。

1)如果该文件已在使用,不能修改它,但你可以尝试其他的东西*A RACE CONDITION*可以做到这一点:

# RACE CONDITION LOOP 
# The only way you have here is to setup a RC loop to check if/when the file can be written. 
$Path = dirname (__FILE__) . "path_to_your_file/out.csv"; 
$tries = 99; // Any number of tries you want. 
for ($i = 1; $i < $tries; $i++) 
{ 
    $output = array_slice ($input, 100); 
    { 
     if (@file_put_contents ($Path , implode ("\n", $output))) 
     { 
      echo "Content Added!"."<br>"; 
      $i = $tries; 
     } 
     else 
     { 
      echo "Content Cannot be Added!"."<br>"; 
     } 
    } 

    ob_flush (); 
    echo "We Have tried " . $i . " times to add content to the file"."<br>"; 
    sleep (1); 
    flush (); 
} 

2)如果文件没有正确的权限,您可以将其设置在您的代码中:

​​

3)如果文件不存在,你可以创建它,但首先一定要检查它是否在那儿:

# Make sure the file that contain this code have the right permissions, user/group . 
# If not this cannot create the file and will not allow you execute the whole code . 
$Path = dirname (__FILE__) . "path_to_your_file/out.csv"; 
$Mode = 0755; // permissions wanted 
$Check_File = file_exists ($Path); 
if ($Check_File) { $File_Exist = true; } 
else { $File_Exist = false; } 

if ($File_Exist) 
{ 
    @touch ($Path); 
    @chmod ($Path, $Mode); 
} 
else 
{ 
    echo "File Already Exist"; 
} 

我希望这可以帮助,如果没有请注明究竟是什么问题。

PS:对于内存限制错误,某些服务器和/或主机提供程序不允许在用户的php.ini或php中设置此值。你必须编辑php和apache的全局配置文件,或者在你使用大量数据库的情况下编译mysql的全局配置文件,以允许最大选择的值限制内存。