BRADERY Posted November 17, 2010 Share Posted November 17, 2010 I know there has to be something to make this script read the whole file.. I have a script that is supposed to implode some stuff in front of an id.. I have a text file with 1.18 million ids that I need the script to read and implode the ids to a new file, well.. the script only reads to the 39k mark and then the script closes.. anyway to stop this from happening? Here is the code <?php $filename = "ids.txt"; $filename2 = "text2.txt"; $file = file($filename, FILE_SKIP_EMPTY_LINES); foreach($file as $cwb) { $cwb = trim($cwb, "\n\r"); $ids[] = "\$id_to_save[] = ".$cwb.";"; } file_put_contents($filename2,implode($ids,"\n")); ?> Quote Link to comment https://forums.phpfreaks.com/topic/218989-script-doesnt-even-read-a-20th-of-the-file/ Share on other sites More sharing options...
BRADERY Posted November 17, 2010 Author Share Posted November 17, 2010 its like it times out reading the file Quote Link to comment https://forums.phpfreaks.com/topic/218989-script-doesnt-even-read-a-20th-of-the-file/#findComment-1135672 Share on other sites More sharing options...
ignace Posted November 17, 2010 Share Posted November 17, 2010 Using file to read out such an amount of lines into memory means you'll need LOTS of RAM (and have it assigned to PHP). A better approach would be to read a "line", process it and continue. $fids = fopen('ids.txt', 'r'); $fsids = fopen('text2.txt', 'w'); while($line = fgets($fids)) { // assuming each ID is newline seperated $line = trim($line); fwrite($fsids, "\$id_to_save[] = $line;\r\n"); } fclose($fsids); fclose($fids); However what you are trying to do won't work, you will never be able to load that file because $id_to_save array would be too massive to load into memory. What are you trying to do quite possibly we can give you a solution that will work. Quote Link to comment https://forums.phpfreaks.com/topic/218989-script-doesnt-even-read-a-20th-of-the-file/#findComment-1135691 Share on other sites More sharing options...
BRADERY Posted November 17, 2010 Author Share Posted November 17, 2010 that works! Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/218989-script-doesnt-even-read-a-20th-of-the-file/#findComment-1135733 Share on other sites More sharing options...
BRADERY Posted November 17, 2010 Author Share Posted November 17, 2010 now, is there anyway to like.. seperate the ids into sets of 20k and write each 20k to a text? so I would have 50 txt files each with 20k ids?! Quote Link to comment https://forums.phpfreaks.com/topic/218989-script-doesnt-even-read-a-20th-of-the-file/#findComment-1135734 Share on other sites More sharing options...
ignace Posted November 17, 2010 Share Posted November 17, 2010 Try that: $fids = fopen('ids.txt', 'r'); $fc = 0; $c = 0; $fsids = fopen('f'.$fc.'.txt', 'w'); fwrite($fsids, "<?php\r\n\r\n"); while($line = fgets($fids)) { // assuming each ID is newline seperated if($c == 20000) { fclose($fsids); $c = 0; // reset ++$fc; $fsids = fopen('f'.$fc.'.txt', 'w'); fwrite($fsids, "<?php\r\n\r\n"); } fwrite($fsids, "\$id_to_save[] = $line;\r\n"); ++$c; } fclose($fsids); fclose($fids); Quote Link to comment https://forums.phpfreaks.com/topic/218989-script-doesnt-even-read-a-20th-of-the-file/#findComment-1135773 Share on other sites More sharing options...
BRADERY Posted November 17, 2010 Author Share Posted November 17, 2010 $id_to_save[] = 150155 ; $id_to_save[] = 38374951 ; $id_to_save[] = 19564418 ; $id_to_save[] = 150898002 ; $id_to_save[] = 24694405 ; $id_to_save[] = 38269678 ; $id_to_save[] = 95273763 ; $id_to_save[] = 14704666 ; $id_to_save[] = 285455647 ; $id_to_save[] = 40976070 ; that was the output.. how do I trim it up Quote Link to comment https://forums.phpfreaks.com/topic/218989-script-doesnt-even-read-a-20th-of-the-file/#findComment-1135834 Share on other sites More sharing options...
sasa Posted November 18, 2010 Share Posted November 18, 2010 add $line = trim($line); before write $line in file Quote Link to comment https://forums.phpfreaks.com/topic/218989-script-doesnt-even-read-a-20th-of-the-file/#findComment-1135941 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.