doupai8095 2016-06-14 17:44
浏览 48
已采纳

有一个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

  • 写回答

2条回答 默认 最新

  • dsfs587465 2016-06-14 18:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题