On Fri, 2010-05-21 at 14:03 +0200, Anton Heuschen wrote:

> Hi Im trying do something like this, have a function which uploads my
> file and returns file pointer ... but at same time ... I want to
> remove all Blank lines in a file and update it before it goes to the
> final location ...
> 
> What I tried was to do a write of file and use some regexp replace to
> remove a blank ... either I am not doing the replace correct or my
> understanding of the file buffer and what I can do with it between the
> browser and saving is not correct,
> 
> Anyway my code looks something like this :
> 
> 
>  $uploadfile     = $this->uploaddir;
>             $mtran          = mt_rand(999,999999);
>             $NewName        = date("Ymd_Gis").$mtran.".csv";
>             $uploadfile     = $uploadfile.$NewName;
> 
>             try{
>                 if
> (move_uploaded_file($_FILES['attachfile']['tmp_name'], $uploadfile))
>                 {
>                     $handle = fopen($uploadfile, "r+");
>                     $lines  = file($uploadfile,
> FILE_SKIP_EMPTY_LINES); //FILE_IGNORE_NEW_LINES |
>                     foreach ($lines as $line_num => $line) {
>                         $line =
> preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", $line);
>                         if(strlen($line) > 0)
>                             $line=trim($line);
>                             $line=$line."\n";
>                             fwrite($handle, $line);
>                     }
>                     fclose($handle);
> 


If the files aren't too large in size, what about using something like
file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
which should pull into an array only those lines with content, and then
just write that back out to the same file line by line?

Thanks,
Ash
http://www.ashleysheridan.co.uk


Reply via email to