Affiliate Marketing
Forum Search

Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)  
Old 21-03-06
scifind's Avatar
thin[box]king
 
Join Date: Aug 2003
Location: Cambridge
Posts: 1,844
Thanks: 8
Thanked 4 Times in 4 Posts
scifind is an unknown quantity at this point
  Split a text file into 2 equal portions

I have a large text file that i wish to process with PHP.
It makes the server generate a 500 error, but if I manually cut the file down to size the script works, is there an easy way for changing:

file.txt
to
file_1.txt and
file_2.txt

where file_1 and file_2 have equal line counts.

Thanks
__________________
Earn an average of £45 per sale. | New Star Trek Trailer | Looking for Mobile Phone Link Swaps
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 21-03-06
Registered User
 
Join Date: Mar 2004
Location: Reading, UK
Posts: 301
Thanks: 0
Thanked 0 Times in 0 Posts
dmorison is an unknown quantity at this point
The following PHP should be able to do it; but there may be a catch 22 if the timeout is a file access issue rather than being related to the processing that you are doing. PHP will need write access to the current directory.

PHP Code:
<?php
  $source 
"file.txt";
  
$destination1 "file_1.txt";
  
$destination2 "file_1.txt";

  
// count the number of lines in $source
  
$lines 0;
  
$fs fopen($source,"r");
  while(!
feof($fs))
  {
    
fgets($fs,1024);
    
$lines++;
  }

  
// divide $count by 2 to get the halfway point
  
$count intval($count 2);

  
// rewind $source and open $destination1 & 2 for writing
  
rewind($fs);

  
$fd1 fopen($destination1,"w");
  
$fd2 fopen($destination2,"w");

  
$progress 0;
  
  while(!
feof($fs))
  {
     
$line fgets($fs,1024);
     
$progress++;
     if (
$progress $count)
     {
        
fwrite($fd1,$line);
     }
     else
     {
        
fwrite($fd2,$line);
     }
  }

  
fclose($fs);
  
fclose($fd1);
  
fclose($fd2);
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 21-03-06
eskimo's Avatar
Registered User
 
Join Date: Aug 2003
Posts: 659
Thanks: 0
Thanked 0 Times in 0 Posts
eskimo is an unknown quantity at this point
nice script, but is this line correct?

Quote:
// divide $count by 2 to get the halfway point
$count = intval($count / 2);
shouldn't it be
Quote:
// divide $count by 2 to get the halfway point
$count = intval($lines / 2);
probably just me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 21-03-06
Registered User
 
Join Date: Mar 2004
Location: Reading, UK
Posts: 301
Thanks: 0
Thanked 0 Times in 0 Posts
dmorison is an unknown quantity at this point
No, it's not just you, it's a bug...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirecting ASP to another file on Apache DanielCoe Widgets, Coding, AJAX, PHP - Technology & Affiliate Marketing 1 04-08-04 03:14 PM
Dell text link will 68 TradeDoubler 2 12-08-03 04:42 AM


Affiliate Marketing RSS Feeds - Contact Us - Affiliate Marketing - Archive - Privacy Statement - Top

Content Relevant URLs by vBSEO 3.2.0 RC7