Reverse the order of fwrite

0

Guys, anyone give me this help, please! I have a form where people go through youtube links containing songs and will generate for me a list of these songs in the music.html file.

How can I reverse the listing of these songs? The .html file inserts each YouTube link below the other, my need is for it to list one over the other, so it makes it easier not to have to scroll down the list in order to reach the last links sent.

    <form action="" method="POST">
        <input name="link" type="text" placeholder="Link do Youtube" />
        <input type="submit" name="submit" value="Enviar">
    </form>

    <?php
        if (!empty($_POST["link"])) {

            $mus = $_POST['link'];

            $arquivo = "musicas.html";
            date_default_timezone_set('America/Bahia');
            $data = date('d/m/Y H:i:s', time());

            $fp = fopen($arquivo, "a+");   
            fwrite($fp,"Data: $data | Link: <a href=$mus>Click Aqui</a><br><br>");   
            fclose($fp);

            echo "Música enviada com sucesso !";

        }
    ?>

Extra Doubt :

How do I blow this Youtube link to modify the video link and take the link to a site to download the .mp3 video?

  

Example - > www.youtube.com/v=AbCdEFgh _

     

Explode with the modifications - > www.yout.com/v=AbCdEFgh _

Yout.com allows you to download .mp3 and .mp4 videos, so when I access my music.html file, I will have this result:

  

Date : 12/04/2016 17:51:21 | Link : Click Here | Download : Click Here

Thank you

    
asked by anonymous 12.04.2016 / 23:05

1 answer

1

You want to insert the data at the beginning of the file, you can use this function.

function prepend($string, $filename) {
  $context = stream_context_create();
  $fp = fopen($filename, 'r', 1, $context);
  $tmpname = md5($string);
  file_put_contents($tmpname, $string);
  file_put_contents($tmpname, $fp, FILE_APPEND);
  fclose($fp);
  unlink($filename);
  rename($tmpname, $filename);
}

EXTRA DUVIDA

You also need to get the value of the href attribute from the <a> tag, so you can use the preg_match ()

$link  =   "<a href='www.youtube.com/v=eXEmPLo_'>video</a>";

preg_match('/(?<!_)href=([\'"])?(.*?)\1/',$link, $matches);

print_r( $matches );

Resulting in:

  

Array ([0] = > href = 'www.youtube.com / v = eXEmPLo_' [1] => '[2] = >   www.youtube.com/v=eXEmPLo_)

I hope that helps you.

    
12.04.2016 / 23:10