imagepng filename from array

qlithe

Supreme Member
Joined
Feb 14, 2012
Messages
1,252
Reaction score
288
This is driving me crazy. It's obviously not the entire code but it illustrates my problem.

PHP:
<?php

$filenames = file('filenames.txt');

for ($i = 0; $i <= count($filenames)-1; $i++)
{
    $filename = $filenames[$i];

    imagepng($im, $filename . '.png');
    imagedestroy($im);
}

?>

For some reason, it refuses to output the files correctly when I use $filename as the 'to' parameter in imagepng. When the code looks like above, only one file is created with the last filename.

But changing the line to ($filename to $i):

PHP:
imagepng($im, $i . '.png');

Works just perfect. All files are created named 0.png, 1.png etc
 
Because there is space at end of line.

Solution is php function trim();
imagepng($im, trim($filename) . '.png');
 
Back
Top