1

I have a form my company uses to create some files on the fly based on the input. I have a text area$fileString and they can enter file names followed by a hard return. I take that and create an array, $list. What i'm trying to do is if they enter in the text area two file names like this:

item1

item2

that it will create an array that contains 6 values, not two, so like this:

$genList = array(item1_f, item1_b, item1_i, item2_f, item2_b, item2_i);

I am getting this error though when I run my code:

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 24 bytes) in /home/dcolor/public_html/dev/create.php on line 18

Line 18 is:

array_push($genList, $list[0] . "_f", $list[0] . "_b", $list[0] . "_i");

What am I doing wrong here? Code below

    $fileString = $_POST['fileList'];
    $unique = $_POST['unique'];
    $size = $_POST['size'];
    $generateArray = $_POST['generateArray'];
    $list = explode("\r\n",$fileString);

    if ($generateArray == "yes") {      
        if ($size == "5x7inimpos") {
            $genList = array();
            while (!empty($list)) {             
                array_push($genList, $list[0] . "_f", $list[0] . "_b", $list[0] . "_i");                    
            }
        }
        print_r($genlist);
    }
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
thindery
  • 1,164
  • 4
  • 23
  • 40

3 Answers3

3
while (!empty($list)) {             
        array_push($genList, $list[0] . "_f", $list[0] . "_b", $list[0] . "_i");                    
}

$list never gets emptied, you never get out of the while loop.

You just want to iterate over the list:

foreach($list as $element)
{

    array_push($genList, $element . "_f", $element . "_b", $element . "_i");                    

}
moonwave99
  • 21,957
  • 3
  • 43
  • 64
2

The problem is

while (!empty($list)) {             
    array_push($genList, $list[0] . "_f", $list[0] . "_b", $list[0] . "_i");                    
}

Because $list will never be empty and thus causing an endless loop. PHP tries to push more and more elements into $genList until the memory limit exceeds.

You probably want to remove the element in the loop.

Daniel M
  • 3,369
  • 20
  • 30
  • I don't understand why the `$list` will never be empty. If the original text area only had 4 file name values, `$list` would contain 4 array values. It would go empty after the 4th go through, correct? – thindery Sep 17 '12 at 16:11
  • 2
    No, because in the While loop you are not removing anything from list. You are reading the value in $list[0] an infinite amount of times :). In this case you could just iterate over the list like in moonwave99's answer. – Terry Seidler Sep 17 '12 at 16:13
1

This looks like an infinite loop to me. Why would $list ever become empty?

while (!empty($list)) {             
    array_push($genList, $list[0] . "_f", $list[0] . "_b", $list[0] . "_i");                    
}

Therefore, you will allocate memory with array_push until you hit your [256MB] memory limit.

jimp
  • 16,999
  • 3
  • 27
  • 36