有一个PHP添加链接到HTML并放入TXT

So I have This Code:

<div class="item">
    <a href="Link to image">
  <img src="Image Source" height="268"/></a>
</div>

I want to make it so i can have a html form that says "Put Image Link Here" and then when you click submit it will add it into the top of a txt file.(above other Sections of html) So Like This:

Add Your Image:
<form>
  Raw Image File:
  <input type="text" name="firstname" value="Raw Image">
  <br>Link to image Page:
  <input type="text" name="lastname" value="Image Page">
  <br>
  <input type="submit" value="Submit">
</form>

and when you click submit it adds it to the txt and redirects you to a successful page thank you

This is very insecure, but if you're just trying to learn the basics of how some of this stuff works, then I hope that this is a helpful demonstration.

You can use file_put_contents to write to files on disk. Be cognizant of operating system file permissions for the user account executing the script (web server). I use the /tmp directory for this example because it usually exists on linux operating systems with global read/write permissions.

Make sure to use the FILE_APPEND flag.

If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.

<?php

$file = '/tmp/some.file.name.html';

/*
  $_GET['lastname'] is your "Link to image Page"

  please change the `name` to something more appropriate

  <input type="text" name="lastname" value="Image Page">
*/

if(isset($_GET['lastname'])){

  $string_to_write = '
    <div class="item">
        <a href="'.$_GET['lastname'].'">
      <img src="Image Source" height="268"/></a>
    </div>
  ';

  // if our file doesn't exist, then create it
  if (!file_exists($file))
    if(!touch($file))
      trigger_error('ERROR: could not create file on disk', E_USER_ERROR);

  // Write the contents to the file, 
  // using the FILE_APPEND flag to append the content to the end of the file
  // and the LOCK_EX flag to prevent anyone else writing to the file at the same time
  if (!file_put_contents($file, $string_to_write, FILE_APPEND | LOCK_EX))
    trigger_error('ERROR: could not write to file on disk', E_USER_ERROR);

  // redirect to wherever
  header('Location: /');

}

// print the file to screen
include($file);

?>

Add Your Image:
<form>
  Raw Image File:
  <input type="text" name="firstname" value="Raw Image">
  <br>Link to image Page:
  <input type="text" name="lastname" value="Image Page">
  <br>
  <input type="submit" value="Submit">
</form>

I'll leave the "Image Source" part for you. Uploading files is a different question.

<?php
 if (isset($_POST['link']))
   { echo "Your link is ".$_POST['link']; }
    /* header('Location: url');  */
     /* uncomment the above line for redirect */   
 ?>
<html>
  <head>
   <title>Sample Application</title>
  </head>
 <body>
   <br>Add Your Image:
    <form action="index.php" method="post">
     Link to Image page: <input type="text" name="link">
     <input type="submit" value="Submit">
    </form>
 </body>
</html>

If i understood your question correctly, you are trying to add URL (Entered by user) to the HTMl text, on clicking submit.
use header for redirecting

header('Location: url');