Jump to content

script doesnt even read a 20th of the file


BRADERY

Recommended Posts

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"));
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

 

 

$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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.