PHP File Operations Help

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
Hey guys, I'm trying to re-work a script written by someone (i forget their name, they are from x10)

It gets the current song name and song length and outputs it on an image. Ok, great, however, since the headers are already sent, you can't update the image (i think... lol).

I tried to re-work it with text, but I'm pretty sure I failed miserably.

Heres my code:

PHP:
<?PHP
require_once("song.txt");
require_once("length_remaining.php");
require_once("song_temp.txt");
$file = song.txt;
$next = song_temp.txt;
$fp = @fopen($file, 'r');
$song = fread($fp, 512);
fclose($fp);
 
list($song, $length) = explode("\n", $song);
 
$length = explode(':', $length);
$length = (intval($length[0]) * 60) + intval($length[1]);
$length -= length() - filemlength($file);
if ($length < 1) {
echo "Between songs or offline";
}
if (strlen($song) > 32) {
 $p = strrpos(substr($song, 0, 32), ' ');
 $song = array(trim(substr($song, 0, $p)), trim(substr($song, $p)));
 if (strlen($song[1]) > 32) {
  $song[1] = substr($song[1], 0, 32) . '...';
echo $song[1];

 }
echo $song;
}

}
else {
 $song = array($song, '');
}
//getnew... i hope

if ($length <= 0) {
$fp = @fopen($next, 'r');
$nextsong = fread($fp, 512);
fclose($fp);
 
list($nextsongsong, $length) = explode("\n", $song);
 
$length = explode(':', $length);
$length = (intval($length[0]) * 60) + intval($length[1]);
$length -= length() - filemlength($file);
if ($length < 1) {
echo "Between songs or offline";
}
if (strlen($nextsong) > 32) {
 $p = strrpos(substr($nextsong, 0, 32), ' ');
 $nextsong = array(trim(substr($nextsong, 0, $p)), trim(substr($nextsong, $p)));
 if (strlen($nextsong[1]) > 32) {
  $nextsong[1] = substr($nextsong[1], 0, 32) . '...';
echo $nextsong[1];

 }
echo $nextsong;
}
}
else {
 $nextsong = array($nextsong, '');
 }
}
?>

and time_remaining.php:

PHP:
<?php
//time_remaining.php
if (!isset($length)) {
 $file = 'song.txt';
 $fp = @fopen($file, 'r');
 $length = fread($fp, 512);
 fclose($fp);
 $length = explode("\n", $length);
 $length = $length[1];
}
$length = explode(':', $length);
$length = (intval($length[0]) * 60) + intval($length[1]);
$length -= time() - filemtime($file);
if (isset($_GET['show'])) {
 print $length;
}
?>

If you could help, that would be great. I am in a rush, so i could probably do this normally...but. yeah.

Its probably a very simple error, or just me being a noob at f-op.

Any help is appreciated, if you give a me a new code that will do the same thing, 300 credits (or more depending on..well...stuff)

Thanks a bunch,
Neil
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
Change lines 5+6 of the first page to this:
PHP:
$file = 'song.txt';
$next = 'song_temp.txt';

EDIT: I can't see anything else wrong. But don't quote me on that. I'm on Vista and don't have my syntax highlighting that I'm used to on Linux! rofl.
Well, other than the fact that you're not outputting it as an image :p

You can output an image by doing something along the lines of the following:
PHP:
<?php
header("Content-type: image/png");
$image = imagecreatetruecolor(550, 200) or die("Cannot Initialize new GD image stream");

$col_transparant = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagecolortransparent ($image, $col_transparant);
$col_black = imagecolorallocate($image, 0, 0, 0);

imagefill($image, 0, 0, $col_transparant);
imagestring($image, 5, 5, 10,  "track: $track", $col_black);
imagestring($image, 5, 5, 50,  "length: $length", $col_black);

imagepng($image);
imagedestroy($image);
and that will output a simple image, assuming that $track and $length are set :)

You may also wish to create a 'template' image, which is a fancy pre-made image to use as a base.
Just keep a space to output the text you desire, then position it correctly in your code 8)
 
Last edited:
T

themasterrocker

Guest
No, because the file is song.txt but the song_temp.txt tells the software we use how to show whats going into the song.txt so i'm sure that bit is right.
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
yeah, i don't want an image, I am most likely going to store it into a flat file db and recall the last like 5 songs or something. I'm trying to get someting like x10radio has. xD
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
lol, you said you wanted it as an image! :)

Anyhoo, like I said on lines 5+6 when you set the two variables, they need to be strings.
PHP:
$file = "myfile.txt";
 
Top