-1

Like always, I am having issues doing this. So, I have been able to extract images from folders using php, but since it was taking too long to do that, I decided to just extract the names from a text file and then do a while loop and echo the list of results. For example I have a file called, "cats.txt" and inside the data looks like this.

Kitten1.jpg
Kitten2.jpg
Kitten3.jpg

I could easily use an sql or json table to do this, but I am not allowed. So, I only have this.

Now, I have tried doing this, but I get an error.

--- PHP CODE ---

$data = file ("path/to/the/file/cats.txt");
for ($i = 0; $i < count ($data); i++){
 $images = '<img src="/kittens/t/$i">';
}

$CatLife = '
Images of our kittens:<br>

'.$images.'


';

I would really appreciate the help. I am not sure of what the error is, since the page doesn't tell me anything. It just doesn't load.

Divern
  • 343
  • 2
  • 15
  • 1
    http://stackoverflow.com/questions/13246597/how-to-read-a-file-line-by-line-in-php – Progrock Feb 01 '17 at 20:41
  • Problem is you are making an images string, storing your counter as file name, and clobbering `$images` on each iteration. You want `$data[$i]`, or to use a foreach, as `$data` is an array of lines from your file. You need to concat. your cats! – Progrock Feb 01 '17 at 20:48
  • Progrock yeah, I just did something similar. '.$data[$i].' and now it works. I also had to specify the full path of the folder. – Divern Feb 01 '17 at 20:50

4 Answers4

1

You can try something like this:

$fp = fopen('path/to/the/file/cats.txt', 'r');
$images = '';
while(!feof($fp)) {
     $row = fgets($fp);
     $images .= '<img src="/kittens/t/'.$row.'">';
}
fclose($fp);

$CatLife = "Images of our kittens:<br>$images";
aperpen
  • 718
  • 7
  • 10
  • 1
    Do or do not. There is no "try". A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Feb 01 '17 at 20:57
0

Get used to foreach(). Much easier. Also note that variables like $file in your img tag don't get interpreted in single quotes. I use an array and then implode it:

$data = file ("path/to/the/file/cats.txt");
foreach($data as $file) {
    $images[] = '<img src="/kittens/t/'.$file.'">';
}

$CatLife = '
Images of our kittens:<br>

'.implode($images).'


';

You could also just use $images .= '<img src="/kittens/t/$file">'; to concatenate and not need to implode.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

Maybe you should use the glob php function instead of parsing your txt file.

foreach (glob("path/to/the/file/Kitten*.jpg") as $filename) {
    echo "$filename\n";
}
Stv
  • 496
  • 6
  • 16
-1
    $data = file ("path/to/the/file/cats.txt");
    $images = '';
    for ($i = 0; $i < count ($data); i++){
         $images .= '<img src="/kittens/t/$i">');
    }

    $CatLife = 'Images of our kittens:<br>'.$images.'';
    echo $CatLife;

Try this, it stores each image tag into a string and echos it to the page.

Loveen Dyall
  • 824
  • 2
  • 8
  • 20