[PHP] Trying to read a file
Well, the file is actually reading fine. But what I want it to do isn't working fine. I have a file (http://necrotic.anitrade.net/list.txt) that I need to read and have spit into seperate files accordingly. Heres what needs to happen:
A line such as [Audio Books]
' creates a folder (./albums/Audio Books). That works 100% fine.
A line such as Lord Of The Rings Audio Books
' should create a file (./albums/Audio Books/Lord_Of_The_Rings_Audio_Books). Works 100% fine.
Now comes the problem parts:
A line such as J.R.R. Tolkien - The Hobbit (--aps)
' Needs to have two things happen: 1) Put into var $album, works. 2) Ignored as far as making/editing a file is concerned. Does not work.
A line such as Disc 01 of 10 23 60.91MB 118,130,VBR
' needs 3 things to happen: 1) "Disc 01 of 10" to be removed and added to sperate var $disc, works. 2) "118,130,VBR" removed and added to seperate var $bitrate, works. Taken out of process for now to reduce runtime for testing. 3) A line (Disc 01 of 10\t118,130,VBR) to be edited into the Artist file (in this case, ./albums/Audio Books/Lord_Of_The_Rings_Audio_Books). Doesn't work.
A line such as 12 stones - 12 stones 12 73.03MB 235,263,VBR
' To have 3 things happen: "12 stones - 12 stones" removed, added to var $artist. Working. 2) "235,263,VBR" added to var $bitrate. Working. 3) File created (./albums/Rock/12_stones_-_12_stones). Working.
Here's my code: http://necrotic.anitrade.net/mp3db.php
<?php
$fp = fopen(\"list.txt\",\"r\");
$file = fread($fp,filesize(\"list.txt\"));
fclose($fp);
$contents = explode(\"\n\",$file);
/* Folder Format:
[GENRE]
BAND
ALBUM
DISC
*/
makediralbums();
echo \"<pre>\";
foreach($contents as $foo2) {
if ($foo2 == '°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°') {
// Do Nothing
} else if ($foo2 == '!CD ') {
// Do Nothing
} else if ($foo2 == 'Artist/Album Files Size Bitrate(s)') {
// Do Nothing
} else if ($foo2 == 'Status: ') {
// Do Nothing
} else if ($foo2 == '¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯') {
// Do Nothing
} else if (preg_match(\"/Total Files:/\",$foo2)) {
// Do Nothing
} else if (preg_match(\"/Total Size:/\",$foo2)) {
// Do Nothing
} else if ($foo2 == '') {
// Do Nothing
} else if (preg_match(\"/List Created In:/\",$foo2)) {
// Do Nothing
} else if (preg_match(\"/List created with/\",$foo2)) {
// Do Nothing
} else {
// Hey, its actually a folder! Do some stuff
// Some folders have files, remove the extra info.
if (substr($foo2,0,69) != '') {
$foo2 = substr($foo2,0,68);
}
if (substr($foo2,0,1) == '[') { // Its the [GENRE] folder, get the genre out.
$foo2 = trim($foo2);
$howlong = strlen($foo2)-2; // How long is the genre name? -2 becase of the square brackets
$genre = substr($foo2,1,$howlong); // Get the genre
makedir($genre);
} else if (substr($foo2,0,strlen(' ')) == ' ') { // Its an ARTIST folder
$foo2 = trim($foo2);
$artist = str_replace(\" \",\"_\",$foo2);
makefile($genre,$artist);
} else if (substr($foo2,0,strlen(' ')) != ' ') { // Its an ALBUM or DISC folder
if (substr($foo2,0,strlen(' ')) == ' ') { // Its a DISC folder
$foo2 = trim($foo2);
$tdisc = \" \".$disc;
editfile($artist,$genre,$tdisc,\"N/A\");
} else { // Its an ALBUM folder
if (strlen($foo2) < '79') $nomakefile;
$foo2 = trim($foo2);
$album = str_replace($erase,\"\",$foo2);
if (!$nomakefile) {
editfile($artist,$genre,$album,\"N/A\");
}
}
}
}
}
echo \"</pre>\";
/*
FUNCTION: makedir
DOES: Creates directory $tomake if not exists
*/
function makedir($tomake) {
if (@!opendir(\"./albums/\".$tomake.\"/\")) {
if (!@mkdir(\"./albums/\".$tomake.\"/\")) {
echo \"Error: mkdir(\".$tomake.\") failed!\n\";
}
@chmod(\"./albums/\".$tomake.\"/\",0777);
echo \"Folder albums/$tomake created!\n\";
}
}
/*
FUNCTION: makediralbums
DOES: Creates directory albums if not exists
*/
function makediralbums() {
if (@opendir(\"./albums/\")) {
$new_name=\"albums_\".mktime();
@rename(\"./albums/\",\"./\".$new_name.\"/\");
}
if (!@mkdir(\"./albums/\")) {
echo \"Error: mkdir(albums) failed!\n\";
}
@chmod(\"./albums/\",0777);
echo \"Folder albums created!\n\";
}
/*
FUNCTION: makefile
DOES: Creates file $tomake if not exists
*/
function makefile($folder,$tomake) {
if (@!is_file(\"./albums/\".$folder.\"/\".$tomake)) {
if (!$fp = @fopen(\"./albums/\".$folder.\"/\".$tomake,\"a+\")) {
echo \"Error: create file \".$folder.\"/\".$tomake.\" failed!\n\";
} else {
@chmod(\"./albums/\".$folder.\"/\".$tomake,0777);
fclose($fp);
echo \" File $folder/$tomake created!\n\";
}
}
}
/*
FUNCTION: editfile
DOES: Edits $tomake
*/
function editfile($tomake,$folder,$stuff,$bitrate) {
if (@!is_file(\"./albums/\".$folder.\"/\".$tomake)) {
if (!$fp = @fopen(\"./albums/\".$folder.\"/\".$tomake,\"a\")) {
echo \"Error: opening file \".$folder.\"/\".$tomake.\" failed!\n\";
} else {
$contents = $stuff.\"\t\".$bitrate.\"\n\";
fwrite($fp,$contents,strlen($contents));
fclose($fp);
echo \" File $tomake edited! ($contents)\";
}
}
}
?>
I'm using flat-files because my client requires it.
[James Logsdon]
Mark Hensler posted this at 06:53 — 8th July 2003.
He has: 4,048 posts
Joined: Aug 2000
<?php
foreach($contents as $line) {
if (preg_match('/^(\[[^]+\])/', $line, $match)) {
// Its the [GENRE] folder, get the genre out.
// pattern:\"[Audio Books]\"
$genre = $match[1];
makedir($genre);
}
//elseif (substr($foo2,0,strlen(' ')) == ' ') {
elseif (preg_match('/^ ([^ ].*)/', $line, $match) {
// Its an ARTIST/BAND folder
// pattern:\" Lord Of The Rings Audio Books\"
$artist = str_replace(\" \", \"_\", $match[1]);
makefile($genre, $artist);
}
elseif (preg_match('/^ ([^ ].*?)\s+([0-9]{1,2})\s+([0-9]+\.[0-9]+[MmKkBb]{2})\s+(([0-9]{1,3},){1,2}(VBR|MPC)?)/', $line, $match) {
// Its an ALBUM folder
// pattern:\" J.R.R. Tolkien - The Hobbit (--aps)\"
$album = str_replace(' ', '_', $match[1]);
$files = $match[2];
$size = $match[3];
$bitrate = $match[4];
editfile($artist, $genre, $album, $bitrate);
}
elseif (preg_match('/^ ([^ ].*?)\s+([0-9]{1,2})\s+([0-9]+\.[0-9]+[MmKkBb]{2})\s+(([0-9]{1,3},){1,2}(VBR|MPC)?)/', $line, $match) {
// Its a DISC folder
// pattern:\" Disc 01 of 10 23 60.91MB 118,130,VBR\"
$tdisc = str_replace(' ', '_', $match[1]);
$files = $match[2];
$size = $match[3];
$bitrate = $match[4];
editfile($artist, $genre, $tdisc, $bitrate);
}
}
?>
And why does "12 stones - 12 stones" have file/size/bitrate info? That should be an Artist (2 space prefix). I'm not up on my music, but looking at that line, I'd say an Artist called "12 stones" released an Album called "12 stones".
Anyway, I hope the above code can help you out somehow. I'll move on for now untill my head stops spinning.
Mark Hensler
If there is no answer on Google, then there is no question.
necrotic posted this at 14:10 — 8th July 2003.
He has: 296 posts
Joined: May 2002
Yes, the line 12 stones - 12 stones is an album. If I can just get the whole thing to work correctly I'm going to make it so 12 stones - 12 stones makes a file and edits in the correct info. For now, keep it simple
As for the Xeonosaga one, I just don't have it as Xenosaga/Soundtrack/Disc 1 of 2 because the ST is all that's out right now.
What my client wants is after user has added his list, then he can edit the values to fix names that were cut off in the shortness, things like the Xenosaga Soundtrack, and pretty much anything. The list part is so they don't have to add every Album manually. Some of near 3000 or more, so it would be a pain in the butt.
Thanks, though. I'm going to test it now.
EDIT:
First, you forgot another parenthasi ')' (horibel speeling) after the 'else if's. The GENRE else if didn't work, changed it to a substr check for '[' at col 1. The album and disc else ifs don't work, either. Looking at them now see if I can work out something. Thanks for the help though
EDIT:
Kick butt, gott it working 100% correctly so far! Heres the edited code:
<?php
foreach($contents as $line) {
if (substr($line,0,1)=='[') {
// Its the [GENRE] folder, get the genre out.
// pattern:\"[Audio Books]\"
$line = substr($line,0,66);
$length = strlen(trim($line))-2;
$genre = substr($line,1,$length);
//echo \"Make Dir: $genre\n\";
makedir($genre);
}
//elseif (substr($foo2,0,strlen(' ')) == ' ') {
elseif (preg_match('/^ ([^ ].*)/', $line, $match)) {
// Its an ARTIST/BAND folder
// pattern:\" Lord Of The Rings Audio Books\"
$artist = substr($match[1],0,66);
$artist = str_replace(\" \",\"_\",trim($artist));
//echo \" Make File: $artist\n\";
makefile($genre, $artist);
}
elseif ((substr($line,0,5)!=' ')&&(substr($line,0,4)==' ')) {
// Its an ALBUM folder
// pattern:\" J.R.R. Tolkien - The Hobbit (--aps)\"
$album = substr($line,0,66);
$album = str_replace(\" \",\"_\",trim($album));
$bitrate = substr($line,86,11);
$tsub = substr($line,0,6);
//echo \" Edit File 4 Album: $artist (Sbstr: $tsub)\n\";
editfile($artist, $genre, $album, $bitrate);
}
elseif (preg_match('/^ [\\D\\d\\S\\W\\w]/', $line, $match)) {
// Its a DISC folder
// pattern:\" Disc 01 of 10 23 60.91MB 118,130,VBR\"
$tdisc = substr($line,0,66);
$tdisc = \" \".str_replace(\" \",\"_\",trim($tdisc));
$bitrate = substr($line,86,11);
//echo \" Edit File 4 Disc: $artist (Contents: $album ($tdisc) - $bitrate)\n\";
editfile($artist, $genre, $tdisc, $bitrate);
}
}
?>
[James Logsdon]
Mark Hensler posted this at 18:16 — 8th July 2003.
He has: 4,048 posts
Joined: Aug 2000
@#$(*&#@%)*^@#%)*&@%#
I hate this. vB has a problem with PHP highlighting and escape characters. Ticks me off.
<?php
foreach($contents as $line) {
if (preg_match('/^(\[[^]+\])/', $line, $match)) {
// Its the [GENRE] folder, get the genre out.
// pattern:"[Audio Books]"
$genre = $match[1];
makedir($genre);
}
//elseif (substr($foo2,0,strlen(' ')) == ' ') {
elseif (preg_match('/^ ([^ ].*)/', $line, $match) {
// Its an ARTIST/BAND folder
// pattern:" Lord Of The Rings Audio Books"
$artist = str_replace(" ", "_", $match[1]);
makefile($genre, $artist);
}
elseif (preg_match('/^ ([^ ].*?)\s+([0-9]{1,2})\s+([0-9]+\.[0-9]+[MmKkBb]{2})\s+(([0-9]{1,3},){1,2}(VBR|MPC)?)/', $line, $match) {
// Its an ALBUM folder
// pattern:" J.R.R. Tolkien - The Hobbit (--aps)"
$album = str_replace(' ', '_', $match[1]);
$files = $match[2];
$size = $match[3];
$bitrate = $match[4];
editfile($artist, $genre, $album, $bitrate);
}
elseif (preg_match('/^ ([^ ].*?)\s+([0-9]{1,2})\s+([0-9]+\.[0-9]+[MmKkBb]{2})\s+(([0-9]{1,3},){1,2}(VBR|MPC)?)/', $line, $match) {
// Its a DISC folder
// pattern:" Disc 01 of 10 23 60.91MB 118,130,VBR"
$tdisc = str_replace(' ', '_', $match[1]);
$files = $match[2];
$size = $match[3];
$bitrate = $match[4];
editfile($artist, $genre, $tdisc, $bitrate);
}
}
?>
Mark Hensler
If there is no answer on Google, then there is no question.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.