0

I have a blog/vlog where I also share my listening habits from Spotify live. I use a little snippet called Snip (available on Github) that fetches my Spotify stream, creates text files for my data (like track, artist, album and trackID)

Now I wanted to add a link on my webpage to the actual track I am playing using the variable $i for trackID

I've come up with this code thanks to some googling and tips/trix pages:

<?php
$myfile = fopen("Snip_TrackId.txt", "r") or die("Die!"); 
echo "<a href='https://open.spotify.com/track/";
echo fread($myfile,filesize("Snip_TrackId.txt"));
echo "'target='_blank'><img width='18px' height='18px' title='Open with Spotify' src='np_spotify.png'><a/> <- Listen";
fclose($myfile);
?>

This works just fine, but when I close down Spotify or pause, the text files empty themselves and my info box turns blank AND THAT'S THE WAY IT SHOULD BE :). B ut...... With my code above, the link, image and text "<- Listen" is still visible and I've been trying to figure out how I can fix this but I'm stuck. (The link is also not really working as it only points to the part without the trackID)

So now I turn to you. Can you help me with a working solution for this? If not, I will probably just keep things as they are.

0

2 Answers 2

1

you can just check the filesize before reading it:

<?php 
if ((int)filesize("Snip_TrackId.txt") > 0)
{
    $myfile = fopen("Snip_TrackId.txt", "r") or die("Die!");
    echo "<a href='https://open.spotify.com/track/";
    echo fread($myfile, filesize("Snip_TrackId.txt"));
    echo "'target='_blank'><img width='18px' height='18px' title='Open with Spotify' src='np_spotify.png'><a/> <- Listen";
    fclose($myfile);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for solving this for me. I found all three answers working fine. Tried to up-vote all of them, but since Iäm new here that didn't work :) I ended up using the first one, that checks if the file is empty or not.
I also ended up adding an 'else statement' like else { echo "Nothing to see here"; } but I'm not sure I'll use that one. The empty box looks fine to me. Thanks again for the help
0

Before writing a link check file for emptiness for example:

$file = "Snip_TrackId.txt";
if (0 < filesize($file)) {
    $myfile = fopen("Snip_TrackId.txt", "r") or die("Die!"); 
    echo "<a href='https://open.spotify.com/track/";
    echo fread($myfile,filesize("Snip_TrackId.txt"));
    echo "'target='_blank'><img width='18px' height='18px' title='Open with   Spotify' src='np_spotify.png'><a/> <- Listen";
    fclose($myfile);
}

Or if file contains spaces - check read data:

$myfile = fopen("Snip_TrackId.txt", "r") or die("Die!"); 
$data = trim(fread($myfile,filesize("Snip_TrackId.txt")));
if ($data) {
    echo "<a href='https://open.spotify.com/track/";
    echo $data;
    echo "'target='_blank'><img width='18px' height='18px' title='Open with   Spotify' src='np_spotify.png'><a/> <- Listen";
}
fclose($myfile);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.