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.
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);
}
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);
Related
I’m not sure how to accomplish this.
In the index page I have a form (as seen below) where someone can type in Biden, Trump, Obama, etc for example and then press submit and on the next page it will display information about the presidents. I have the index page working as what was not hard at all, however on the next page witch is “find.php” is where I am having some issues getting anything to work.
<form action="find.php" method="POST">
<input type="text" name="potus">
<input type="submit">
</form>
On this page as seen below is the find.php page where I have entered this code in:
<?php
$file = fopen("works.txt", "r") or exit("unable to open file!");
//output a line of the file until the end is reached
while(!feof($file))
{
//will return each line with a break
echo fgets($file). '<br />';
}
fclose($file);
?>
I know what if I add this <?php echo $_POST["potus"]; ?> on the next page it should display what the user entered on the first page witch was “Biden”, but when I add this code to the code that I already have, the page is blank. The code that I have pulls the data from the text file.
<?php
$file = fopen("<?php echo $_POST["potus"]; ?>.txt", "r") or exit("unable to open file!");
//output a line of the file until the end is reached
while(!feof($file))
{
//will return each line with a break
echo fgets($file). '<br />';
}
fclose($file);
?>
Instead of
$file = fopen("<?php echo $_POST["potus"]; ?>.txt", "r") or exit("unable to open file!");
try
$file = fopen($_POST["potus"] . ".txt", "r") or exit("unable to open file!");
I realize we need forms to submit a POST or perhaps with the help of a button. However, how do we do it using tags? Example code:
<?php
$myfile = fopen("pictures.txt", "r") or die("Unable to open file!");
while(!feof($myfile)){
$pic_name= fgets($myfile);
echo '<div class="grid-item">';
echo '<img src="' . $pic_name . '.jpg">';
echo '</div>';
}
?>
I'm trying use PHP to read a text file for a number of variables (names of each picture that I've saved). From the text file, this PHP will then convert the variables into multiple images (for each variables). Upon clicking the image, this should send the name of the picture to the next PHP site so the picture is displayed in full screen. Assume this is some kind of photo library thing that I'm trying to do. I've tried searching elsewhere but couldn't get an answer for this.
you can use get method instead for sending data for eg.
"form.php?name1=value1&name2=value2"
<?php
$myfile = fopen("pictures.txt", "r") or die("Unable to open file!");
while(!feof($myfile)){
$pic_name= fgets($myfile);
echo '<div class="grid-item">';
echo '';
echo '</div>';
}
?>
I need to be able to change text in a text file that is used to generate an HREF attribute of a link in my page through a form (i.e. the user puts the link into form and the link in HTML will change). So far I made that form and one PHP script. It is working, except that it won't just change that HREF attribute but it's making a whole new link (i.e. <img src="web/assets/images/youtube.png" alt="YT" width="42" height="42" border="0">) in the HTML.
Its working like that - when you put link into form, that script will write that link into the TXT file, then on that page where I need to have that HREF is another script that takes out link from a text file. Here is the PHP:
<?php
$subor = #file('youtube.txt');
$lines = sizeof($subor);
$count = $lines/1;
for ($i = 0; $i < $count; $i++)
{
?>
<a href="<?php echo ($subor[$i * 1 + 0]) ?>"><img
src="web/assets/images/youtube.png" alt="YT" width="42" height="42"
border="0"></a>
<?php
}
?>
And this is script that is writing the link from form into the text file:
<?php
$myFile = "youtube.txt";
if(isset($_POST['flag']) && !empty($_POST['flag'])) {
$fileWrite = $_POST['flag'] . "\n";
}
if($fileWrite) {
$fh = fopen($myFile, "a") or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
exec('/your/command /dev/null 2>/dev/null &');
}
?>
<?php
header( 'Location: http://kraj.com/home.php' ) ;
?>
So how to edit that code so it won't create another but it will change the href inside that one ?
Mode parameter
So how to edit that code so it wont create another but it will change href inside that one ?
Change the second parameter (i.e. mode) passed to fopen() to 'w'. That way the pointer will be placed at the beginning of the file, instead of having the pointer at the end of the file (for appending).
$fh = fopen($myFile, "w") or die("can't open file"); //Make sure you have permission
See a demonstration of this in this playground example.
File_put_contents()
A simpler technique would be to use file_put_contents(). Then the following three lines
$fh = fopen($myFile, "a") or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
could be replaced with:
$numberOfBytesWritten = file_put_contents($myFile, $fileWrite);
I'm creating a html template with notification under nav bar , and admin can change that notification from the system the text of notification bar will be from notetxt file from the same location path where index.html is located i ave tried
<?php
foreach (glob("note.txt") as $filename) {
readfile($filename);
}
?>
and many other way but nothing happens it still stay blank
You are not echoing out the content of the textfile.
do it like this:
$myFile = "note.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
This will output your content of the file.
i'm using this code in pure html file
You can't use PHP functions in plain HTML file. MUST be written in a PHP file.
You have now in your code:
<span>
<!--?php
foreach (glob("note.txt") as $filename) {
$fileArr = file_get_contents($filename);
}
?-->
</span>
Try with the examples above in a proper PHP file... then must work.
you can use file_get_contents function,
try something like this :
<?php
foreach (glob("note.txt") as $filename) {
$fileArr = file_get_contents($filename);
}
?>
It's very simple use file_get_contents();
<?= file_exists('note.txt') ? file_get_contents("note.txt") : "file doesn't exists"; ?>
That is all what you need. file_get_contents() get the content of file and returns it. I've also checked if file exists because it may be your problem. Also make sure you have proper rights to read the file(CHMOD) and file is not empty.
I'm using this code now to display a text file on a php/html page.
<?php
foreach (glob("example.txt") as $filename) {
echo nl2br(file_get_contents($filename));
echo "<br></br>";
}
?>
I'm looking for a way to display the example.txt file from another server with URI.
Something like this: http://address.com/dir/example.txt
Is there a simple way to do this?
(I would use an iframe but it's not possible to style the text without Java or JQuery).
You could just use
file_get_contents('http://address.com/dir/example.txt');
You code is totally wrong
foreach (glob("example.txt") as $filename) {
^------------------------- searching for a file
They can only one example.txt file in a folder at a time except you want to get all text files should should be like this in the first place
foreach (glob("*.txt") as $filename) {
If that is not the case the code would work for both remote and local file
error_reporting(E_ALL);
ini_set("display_errors", "on");
$fileName = "example.txt" ; // or http://oursite.com/example.txt
echo nl2br(file_get_contents($fileName));
echo "<br></br>";
You will have to use CURL to fetch the content of the file first and then display it.
Another option is to use iframes and set the target of iframe to the desired text file.
Yet another option is to use ajax to fetch the content from client end as suggested in comment.
Check fopen() / fread() and your available transport wrappers.
For normal length text file you can use:
<?PHP
$file = fopen("http://www.xyz.com/textfile.txt", "rb");
$output = fread($file, 8192);
fclose($file);
echo($output);
echo "<br>";
?>
For longer files:
<?PHP
$file = fopen("http://www.xyz.com/textfile.txt", "rb");
$output = '';
while (!feof($file)) {
$output .= fread($file, 8192);
}
fclose($file);
echo($output);
echo "<br>";
?>
It will prevent packet exceed issues in longer files by concatenating the file together in several groupings using while loop