Reverse the order of fwrite

Asked

Viewed 87 times

0

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

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

    <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 it to a site link to download the . mp3 video ?

Example -> www.youtube.com/v=Abcdefgh_

Explode with modifications -> www.yout.com/v=Abcdefgh_

The site yout.com allows you to download . mp3 and . mp4 of the videos, so when I access my file musicas.html, I will have this result:

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

Thank you

  • You want to take only the value of the href attribute from the link ?

  • That is, to modify the whole link of it: www.youtube.com/v=eXEmPLo_ For this: www.yout.com/v=example_

  • Okay, I’ll post the solution.

1 answer

1

You want to enter 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 DOUBT

You also need to take the value of the href attribute from the tag <a>, for this, you can use the function 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=example_ )

I hope it helps you.

  • Just enter this function above the code I posted ?

  • This function saves a text in a file, but it inserts the newest record in the first line, as if it were a list ordering decreasingly. So when recording the file with the links coming from the form, you use it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.