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.
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";
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.
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";
}
$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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.