469,903 Members | 1,499 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

fopen read and write problem

I'm trying to write a script that will read from a text list of songs
that I burned onto my CD (Albums011.txt), then write to the database
text the new text made ready for inserting into a database.

The entries from the Albums011.txt look like this:
Wayne Newton - Wild Cool & Swingin' - 03 - But Not For Me.mp3
Wayne Newton - Wild Cool & Swingin' - 04 - Wives And Lovers.mp3 etc.

and I want to manipulate it them to look in the database.txt like this:
INSERT INTO `lists` VALUES ( "Albums012", "Wayne Newton - Wild Cool &
Swingin' - 03 - But Not For Me.mp3 3.25MB 192k ");
INSERT INTO `lists` VALUES ( "Albums012", "Wayne Newton - Wild Cool &
Swingin' - 04 - Wives And Lovers.mp3 2.88MB 192k ");

but I can't seem to get the proper syntax to get the result I want. Can
anyone help? Thanks.

The non-working code I've written is below:

<?php

$filename = 'e:\mirc\CDLists\Albums011.txt';
$fp = fopen($filename, "r");
$contents = fread($fp, filesize($filename));
fclose($fp);

$filename = 'e:\mirc\CDLists\database.txt';
$fp = fopen($filename, "a");
$string = $contents;
foreach($string as $key=>$val){
$write = fputs($fp, "INSERT INTO `lists` VALUES (
\"Albums011\",\"$val\"); \n");
}
fclose($fp);

?>

Jul 17 '05 #1
4 2716
On Tue, 25 May 2004 19:50:31 GMT, JDJones <no***@home.com> wrote:

this is pretty simple

<?php

function read_songlist($file)
{
if (!file_exists($file))
{
echo "Don't exist\n";
return false;
}

$fd = @fopen($file, 'r');
if (!is_resource($fd))
{
echo "Error reading\n";
return false;
}

$album = preg_replace('/\.txt$/', '', $file);
$album = mysql_escape_string( $album );

$ret = array();
while ($line = fgets($fd, 4096))
{
$track = mysql_escape_string( trim($line) );
if (strlen($track) > 0)
$ret[] = sprintf("INSERT INTO lists(album,track)
VALUES('%s', '%s');", $album, $track);
}

fclose($fd);
return $ret;
}

// i'll use echo's for the example, if you want to write to a file you
could: php -q [thisfile].php >inserts.sql
// or, you can write your own fopen() fputs() to write an output to a
file

$inserts = read_songlist('Albums011.txt');
if (is_array($inserts))
{
foreach($inserts as $idx => $sql)
echo $sql . "\n";
} else echo "error\n";

?>
NOTE:
AlbumsWHATEVER.txt *must* have one song per/line.
also, you might want to do text trimming to make sure tracks are going
to fit into your tables.... example, if the track row is varchar(100)
then change the $track = mysql_escape_string() line to something like:
$track = substr(trim($line), 0, 100);
$track = mysql_escape_string( $track );
I'm trying to write a script that will read from a text list of songs
that I burned onto my CD (Albums011.txt), then write to the database
text the new text made ready for inserting into a database.

The entries from the Albums011.txt look like this:
Wayne Newton - Wild Cool & Swingin' - 03 - But Not For Me.mp3
Wayne Newton - Wild Cool & Swingin' - 04 - Wives And Lovers.mp3 etc.

and I want to manipulate it them to look in the database.txt like this:
INSERT INTO `lists` VALUES ( "Albums012", "Wayne Newton - Wild Cool &
Swingin' - 03 - But Not For Me.mp3 3.25MB 192k ");
INSERT INTO `lists` VALUES ( "Albums012", "Wayne Newton - Wild Cool &
Swingin' - 04 - Wives And Lovers.mp3 2.88MB 192k ");

but I can't seem to get the proper syntax to get the result I want. Can
anyone help? Thanks.

The non-working code I've written is below:

<?php

$filename = 'e:\mirc\CDLists\Albums011.txt';
$fp = fopen($filename, "r");
$contents = fread($fp, filesize($filename));
fclose($fp);

$filename = 'e:\mirc\CDLists\database.txt';
$fp = fopen($filename, "a");
$string = $contents;
foreach($string as $key=>$val){
$write = fputs($fp, "INSERT INTO `lists` VALUES (
\"Albums011\",\"$val\"); \n");
}
fclose($fp);

?>


Jul 17 '05 #2
Shane Lahey wrote:
On Tue, 25 May 2004 19:50:31 GMT, JDJones <no***@home.com> wrote:

this is pretty simple

<?php

function read_songlist($file)
{
if (!file_exists($file))
{
echo "Don't exist\n";
return false;
}

$fd = @fopen($file, 'r');
if (!is_resource($fd))
{
echo "Error reading\n";
return false;
}

$album = preg_replace('/\.txt$/', '', $file);
$album = mysql_escape_string( $album );

$ret = array();
while ($line = fgets($fd, 4096))
{
$track = mysql_escape_string( trim($line) );
if (strlen($track) > 0)
$ret[] = sprintf("INSERT INTO lists(album,track)
VALUES('%s', '%s');", $album, $track);
}

fclose($fd);
return $ret;
}

// i'll use echo's for the example, if you want to write to a file you
could: php -q [thisfile].php >inserts.sql
// or, you can write your own fopen() fputs() to write an output to a
file

$inserts = read_songlist('Albums011.txt');
if (is_array($inserts))
{
foreach($inserts as $idx => $sql)
echo $sql . "\n";
} else echo "error\n";

?>
NOTE:
AlbumsWHATEVER.txt *must* have one song per/line.
also, you might want to do text trimming to make sure tracks are going
to fit into your tables.... example, if the track row is varchar(100)
then change the $track = mysql_escape_string() line to something like:
$track = substr(trim($line), 0, 100);
$track = mysql_escape_string( $track );


Thanks Shane, this is great. I have added writing it to a new file but
I'm having one problem I can't solve. The files that are being read and
written to aren't in the same directory as the script. I have to change
read_songlist('Albums011.txt') into
read_songlist('e:/mirc/CDLists/Albums011.txt').

But by doing that, the filepath prints out in the finished product:
INSERT INTO `lists` VALUES("e:/mirc/CDLists/Albums010", "Louis Prima &
Keely Smith - Wild Cool & Swingin\' - 09 - Just One of Those Things.mp3
3.56MB 192k");

How can I get rid of the filepath e:/mirc/CDLists/ and leave just the
Albums010 part?

Jul 17 '05 #3
On Wed, 26 May 2004 16:10:37 GMT, JDJones <no***@home.com> wrote:

simple: basename('path');
this will return everything after the last \ or / in a path ((the
filename))
How can I get rid of the filepath e:/mirc/CDLists/ and leave just the
Albums010 part?


Jul 17 '05 #4
On Wed, 26 May 2004 16:10:37 GMT, JDJones <no***@home.com> wrote:
But by doing that, the filepath prints out in the finished product:
INSERT INTO `lists` VALUES("e:/mirc/CDLists/Albums010", "Louis Prima &
Keely Smith - Wild Cool & Swingin\' - 09 - Just One of Those Things.mp3
3.56MB 192k");


another thing you may consider doing is creating a seperate column for
the filesize & for the bitrate

eg

CREATE TABLE `lists` (
`id` int(5) NOT NULL auto_increment,
`cd` varchar(100) NOT NULL default '',
`mp3` varchar(100) NOT NULL default '',
`filesize` varchar(200) NOT NULL default '',
`bitrate` int(3) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM;

then use something like

INSERT INTO lists(cd,mp3,filesize,bitrate) VALUES('Albums010',
'TrackName.mp3', 'xxxMb', 192);

now you can do more usefull queries on your database, for example
to select all mp3's with a bitrate of 192kbps
SELECT * FROM lists WHERE lists.bitrate = 192;

as for variable bitrate songs you could set bitrate to 0
SELECT * FROM lists WHERE lists.bitrate = 0;
.....or get all mp3's on Albums010 where bitrate is 192kbps or VBR ....

SELECT * FROM lists WHERE lists.cd = 'Albums010' AND (lists.bitrate =
192 OR lists.bitrate = 0);

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Martin Lucas-Smith | last post: by
9 posts views Thread by monomaniac21 | last post: by
11 posts views Thread by typingcat | last post: by
10 posts views Thread by Grocery Clerk | last post: by
3 posts views Thread by Patrice | last post: by
13 posts views Thread by Blue | last post: by
10 posts views Thread by pjlsr | last post: by
16 posts views Thread by Hans Fredrik Nordhaug | last post: by
31 posts views Thread by Bill Cunningham | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.