problem writing dynamic value into text file
Hi all. i have a little problem writing dynamic value to a ram.txt file. I want write the following infor int a text file:
The song path for each song id i pass.
<player showDisplay="yes" showPlaylist="yes" autoStart="yes">
and
</player>
are written once at the start and end of the text file.The title is just the name of song without .mp3 extention.
Currently when i call the code like this:
http://localhost/player/ram4.php?sid=1,2,
the only information which is get written to text file is the url but i want write song path and title in above mention format.
I be happy if an expert help me here.Thanks
ram4.php code
Code:
<?php
$url[1] = "http://localhost/flash_mp3_player/mp3/08 - Track 8.mp3";
$url[2] = "http://localhost/flash_mp3_player/mp3/13 - Gar Aya.mp3";
$url[3] = "http://localhost/flash_mp3_player/mp3/Soroush - Yeh Donya - 02 Shoghe Nafas.mp3";
$mycontent = "";
if (isset($_GET["sid"]))
{
$allsid = explode (",",$_GET["sid"]);
$mycontent = array();
foreach ($allsid AS $value)
$mycontent[] = $url[$value];
}
echo $mycontent;
$handle = fopen ("ram.txt","w+");
if ($handle)
{
if (fwrite ( $handle,implode("\r\n",$mycontent)."\r\n") )
{
echo "FILE IS WRITTEN SUCCESSFULLY";
} else
{
echo "ERROR IN WRITING TO FILE";
}
fclose ($handle);
} else
{
echo "ERROR IN OPENING FILE";
}
require 'config.txt';
?>
1 Attachment(s)
Re: problem writing dynamic value into text file
PHP Code:
require_once('ID3v1x.php');
.....
$handle = fopen ("ram.txt","w+");
if ($handle)
{
fwrite($handle,"<player showDisplay=\"yes\" showPlaylist=\"yes\" autoStart=\"yes\">");
foreach($url as $u)
{
$ID3 = new ID3v1x($u);
if($ID3->read_tag() == true)
{
$string = "<song path=\"$u\" title=\"" . trim($ID3->title) ."\" />";
fwrite($handle,$string);
}
else
{
$string = "<song path=\"$u\" />";
}
}
fwrite($handle,"</player>");
fclose ($handle);
}
.....
This will check the tag of the MP3 file and if one exists then it will add it to the file.
The class used is attached (from PHPClasses).
Hope thats what your looking for.
Re: problem writing dynamic value into text file
Quote:
Originally Posted by john tindell
PHP Code:
require_once('ID3v1x.php');
.....
$handle = fopen ("ram.txt","w+");
if ($handle)
{
fwrite($handle,"<player showDisplay=\"yes\" showPlaylist=\"yes\" autoStart=\"yes\">");
[B]foreach($url as $u)[/B]
{
$ID3 = new ID3v1x($u);
if($ID3->read_tag() == true)
{
$string = "<song path=\"$u\" title=\"" . trim($ID3->title) ."\" />";
fwrite($handle,$string);
}
else
{
$string = "<song path=\"$u\" />";
}
}
fwrite($handle,"</player>");
fclose ($handle);
}
.....
This will check the tag of the MP3 file and if one exists then it will add it to the file.
The class used is attached (from PHPClasses).
Hope thats what your looking for.
Many Many thanks for u nice reply:-)) i tried but unfortunetly it writes this part to text file only :
<player showDisplay="yes" showPlaylist="yes" autoStart="yes"></player>
but not the rest!! could u help me fix this problem i get the following errors:
Code:
Array
Warning: fopen(http://localhost/flash_mp3_player/mp3/01 - Saracha.mp3) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in c:\wamp\www\player\ID3v1x.php on line 30
Warning: fopen(http://localhost/flash_mp3_player/mp3/13 - Gar Aya.mp3) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in c:\wamp\www\player\ID3v1x.php on line 30
Warning: fopen(http://localhost/flash_mp3_player/mp3/Soroush - Yeh Donya - 02 Shoghe Nafas.mp3) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in c:\wamp\www\player\ID3v1x.php on line 30
complete code :
Code:
<?php
$url[1] = "http://localhost/flash_mp3_player/mp3/01 - Saracha.mp3";
$url[2] = "http://localhost/flash_mp3_player/mp3/13 - Gar Aya.mp3";
$url[3] = "http://localhost/flash_mp3_player/mp3/Soroush - Yeh Donya - 02 Shoghe Nafas.mp3";
$mycontent = "";
if (isset($_GET["sid"]))
{
$allsid = explode (",",$_GET["sid"]);
$mycontent = array();
foreach ($allsid AS $value)
$mycontent[] = $url[$value];
}
echo $mycontent;
//$filename = time() . ".txt";
//$handle = fopen ($filename,"w+");
require_once('ID3v1x.php');
$handle = fopen ("ram.txt","w+");
if ($handle)
{
fwrite($handle,"<player showDisplay=\"yes\" showPlaylist=\"yes\" autoStart=\"yes\">");
foreach($url as $u) ====> line 30 where error points to
{
$ID3 = new ID3v1x($u);
if($ID3->read_tag() == true)
{
$string = "<song path=\"$u\" title=\"" . trim($ID3->title) ."\" />";
fwrite($handle,$string);
}
else
{
$string = "<song path=\"$u\" />";
}
}
fwrite($handle,"</player>");
fclose ($handle);
}
//$handle = fopen ("ram.exe","w+");
require 'config.txt';
?>
Re: problem writing dynamic value into text file
try changing the url to the relative path of the file. so if the file was in a subfolder called music then it would be
PHP Code:
$url[1] = 'music/01 - Elements.mp3';
and change the "[location of file from web]" to the path that appears if you were to browse to them.
PHP Code:
$string = "<song path=\"http://localhost/[location of file from web]/$u\" title=\"" . trim($ID3->title) ."\" />";
Hope that makes sense
Re: problem writing dynamic value into text file
Quote:
Originally Posted by john tindell
try changing the url to the relative path of the file. so if the file was in a subfolder called music then it would be
PHP Code:
$url[1] = 'music/01 - Elements.mp3';
and change the "[location of file from web]" to the path that appears if you were to browse to them.
PHP Code:
$string = "<song path=\"http://localhost/[location of file from web]/$u\" title=\"" . trim($ID3->title) ."\" />";
Hope that makes sense
Many Many thanks to u that solve the problem. But the only problem is that it writes all the path of songs inside the music folder. For example if i call the script like this :
http://localhost/player/ram4.php?sid=1,2,
it should only write path of song number 1 and 2 but not the whole thing!!!
Re: problem writing dynamic value into text file
PHP Code:
if(!empty($allsid))
{
foreach($allsid as $id) ====> line 30 where error points to
{
$ID3 = new ID3v1x($url[$id]);
if($ID3->read_tag() == true)
{
$string = "<song path=\"{$url[$id]}\" title=\"" . trim($ID3->title) ."\" />";
fwrite($handle,$string);
}
else
{
$string = "<song path=\"{$url[$id]}\" />";
}
}
}
That will use only the value suplied in the $_GET['sid'] and write them to the file.
Re: problem writing dynamic value into text file
john tindell many many thanks this solved that problem. But i want to know how to use mp3 that are in remote server? When i use
$url[5] = "http://remotesite.com/NewMusic/1.mp3";
i get this error :
Code:
Warning: fopen(http://remotesite.com/NewMusic/1.mp3) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in c:\wamp\www\player\ID3v1x.php on line 30
Is there a way to modify ID3v1x.php so it deals with remote mp3 files as well?Furthermore, how about if i do not use title for remote files? since from my understanding ID3v1x.php is only used to get the mp3 title not the song path..I tested by removeing this line
title=\"" . trim($ID3->title) ."\"
it played the local songs but not the remote one.
Correct me if i am wrong . do u think it will solve the problem? i be happy if u help me to be able to deal with remote files.Thanks
Re: problem writing dynamic value into text file
As far as im away fseek which is used by the ID3v1x.php does not support streams. This is the what you get when you open a URL with fopen.
Quote:
http://uk.php.net/manual/en/function.fseek.php
May not be used on file pointers returned by fopen() if they use the "http://" or "ftp://" formats. fseek() gives also undefined results for append-only streams (opened with "a" flag).
How ever the same code be used but the "title" information will not be added.
A fix for the moment is
PHP Code:
if(@$ID3->read_tag() == true)
This will work, ill look to see if i can find away to help you, but it might involve downloading the file from the remote server to your server just so that it can read the tag information from the MP3 file.
Re: problem writing dynamic value into text file
Thank u for u reply. Here is what i did and it played the remote song along with local file but without titles!!:
Code:
//require_once('ID3v1x.php');
if ($handle)
{
fwrite($handle,"<player showDisplay=\"yes\" showPlaylist=\"yes\" autoStart=\"yes\">");
if(!empty($allsid))
{
foreach($allsid as $id)
{
//$ID3 = new ID3v1x($url[$id]);
//if($ID3->read_tag() == true)
//{
$string = "<song path=\"{$url[$id]}\" />";
fwrite($handle,$string);
// }
//else
//{
// $string = "<song path=\"{$url[$id]}\" />";
// } }
}
fwrite($handle,"</player>");
fclose ($handle);
}
The only problem is that it outputed something like this :
Code:
<player showDisplay="yes" showPlaylist="yes" autoStart="yes">
<song path="" />
<song path="music/08 - Track 8.mp3" />
<song path="music/13 - Gar Aya.mp3" />
<song path="music/04- Yeh Donya by sorosh.mp3" />
<song path="music/subfolder/afshane delara.mp3" />
<song path="http://remoteserver.com/NewMusic/remotefile.mp3" />
</player>
I do not know where i get this <song path="" /> ? This line prevents the player to autostart but as u see the remote song is on the playlist without title!
I have this thing in mind but not sure how to code it and i be happy if u help me with it. i want to be able to put the titles of mp3 which are in local server using 'ID3v1x.php'.For the the remote mp3 for example this file:
http://remoteserver.com/NewMusic//remoteSong.mp3
i do not use 'ID3v1x.php' and instead i scan the remote URL and remove the .mp3 extention and copy the song name between / and mp3 extention and use that as song title .
I tried using if(@$ID3->read_tag() == true) as u suggested it only played local file with title and ignoring the local file completly and no warning!! i think this change only removed the warning but not the actual problem.
Thanks
Re: problem writing dynamic value into text file
I was hoping that the @ would supress the error so that it would continue and not include the file that didnt work, o well. Try checked to see if the url is empty of not before you write it to the file.
PHP Code:
if($url[$id] != "")
{
$string = "<song path=\"{$url[$id]}\" />";
fwrite($handle,$string);
}
Re: problem writing dynamic value into text file
Quote:
Originally Posted by john tindell
I was hoping that the @ would supress the error so that it would continue and not include the file that didnt work, o well. Try checked to see if the url is empty of not before you write it to the file.
PHP Code:
if($url[$id] != "")
{
$string = "<song path=\"{$url[$id]}\" />";
fwrite($handle,$string);
}
Thanks that removed the extra empty url but still thining about how get title in to action!