Jump to content

Simple fopen() question


rileypool

Recommended Posts

Hey all, I'm new to this forum and it looks like I'll be spending a lot of time here soon as I'm just now actually starting to LEARN php instead of hacking around various scripts I've used in the past.

 

My question lies with the code below.  I'm reading a .txt file that is full of nothing but urls on every line.

 

I want to read every line from that text file and display that image using HTML.  How would this be done?  This is what I have so far but the url I'm reading from the text file isn't being inserted into the <img> tag.

 

<?php
$file = fopen("http://www.example.com/urls.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached
while(!feof($file))
  {
  $urltxt = fgets($file);
  echo fgets($file). "<img src=" . $urltxt . "><br />";
  }
fclose($file);
?>

Link to comment
Share on other sites

Why not try this:

<?php
$filename = 'file_location/file_name.txt';

$contents = file_get_contents($filename);

$contents = explode($contents, '
' );

foreach($contents as $url)
{
print $url;
echo'<br />';
}




?>

 

 

I haven't tested it... but it looks right to me...

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.