473,503 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2912
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6536
by: Martin Lucas-Smith | last post by:
I wrote the function below as part of a larger class. The fopen stage works, and, as according to the documentation at www.php.net/fopen that succesfully creates a new file. The fwrite stage...
9
2785
by: monomaniac21 | last post by:
Hi everyone i'm trying to setup my website to create new webpages dynamically using php but I am have a major problem in that whenever i create a file it is always created locked so that it can...
11
5771
by: typingcat | last post by:
Is it possible to read another web page in PHP? If is ASP.NET, the code would be ------------ WebRequest req=WebRequest.Create("http://www.microsoft.com"); WebResponse res=req.GetResponse();...
10
32385
by: Grocery Clerk | last post by:
I know open() returns a file descriptor and fopen() returns a pointer to FILE. The question is, when do I use fopen() and when do I use open()? Could someone give me an example when to use one...
3
2569
by: Patrice | last post by:
Hi, I would to call fopen function several time in my application. This application permits to read files which path is registered in a configuration file. For exemple: File 1 = toto.txt File 2...
13
22589
by: Blue | last post by:
Hi , Can any one please let me explain me the diffrences between "open"/ "fopen" or "read"/"fread" or "write/fwrite". I know that "open" /"read" / "write" are system calls and "fopen"...
10
4733
by: pjlsr | last post by:
It's close to twenty years since I used the C language and at that time I was doing only floating point computational work, nothing with strings or reading files. I tried to use fopen in the...
16
4650
by: Hans Fredrik Nordhaug | last post by:
I'm trying to write to a file in the current directory - no remote files. The subject says it all - I can add that both the directory and the file is wordwritable. This happens on a (quite good)...
31
4503
by: Bill Cunningham | last post by:
Is there a real difference in r+ and w+ ? I read in my man pages that r+ opened a file for reading and writing while w+ opened a file for reading and writing and if the file didn't exist it would...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.