JREAM Posted January 2, 2009 Share Posted January 2, 2009 This keeps replacing itself and there are supposed to be a long list of items <?php function list_mp3s () { $directory = 'public/mp3s/'; $results = array(); $handler = opendir($directory); while ($file = readdir($handler)) { if ($file != '.' && $file != '..') { // $filename = RemoveExtension($file); // Use this later $fp = fopen('data.txt', 'w'); $data = "<track>"; $data .= "<location>$directory$file</location>"; $data .= "<image></image>"; $data .= "<annotation>$filename</annotation>"; $data .= "</track>"; fwrite($fp, $data); fclose($fp); } }closedir($handler);} // do it list_mp3s(); ?> I can only get it to write one file, Can anyone help me put it into an array and write it at the end if thats how i do it? Link to comment https://forums.phpfreaks.com/topic/139233-could-anyone-help-me-make-this-write-into-a-file/ Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 $fp = fopen('data.txt', 'a'); Link to comment https://forums.phpfreaks.com/topic/139233-could-anyone-help-me-make-this-write-into-a-file/#findComment-728264 Share on other sites More sharing options...
flyhoney Posted January 2, 2009 Share Posted January 2, 2009 Mchl has the answer. The 'a' flag means to append data to the end of the file instead of overwriting it. Link to comment https://forums.phpfreaks.com/topic/139233-could-anyone-help-me-make-this-write-into-a-file/#findComment-728288 Share on other sites More sharing options...
JREAM Posted January 2, 2009 Author Share Posted January 2, 2009 The 'a' works, but if i refresh the page, it adds it again, when i got: file1.mp3, file2.mp3, and file3.mp3 if i hit refresh ti will do: file1.mp3, file2.mp3, and file3.mp3, file1.mp3, file2.mp3, and file3.mp3 should I do this before i open: // this would have to go outside of the while loop $fp = fopen('data.txt', 'w'); $data = ""; fwrite($fp, $data); fclose($fp); // then the normal part here: while .... $fp = fopen('data.txt', 'a'); $data = "<track>"; $data .= "<location>$directory$file</location>"; $data .= "<image></image>"; $data .= "<annotation>$filename</annotation>"; $data .= "</track>"; fwrite($fp, $data); fclose($fp); Link to comment https://forums.phpfreaks.com/topic/139233-could-anyone-help-me-make-this-write-into-a-file/#findComment-728290 Share on other sites More sharing options...
flyhoney Posted January 2, 2009 Share Posted January 2, 2009 <?php function list_mp3s () { $directory = 'public/mp3s/'; $results = array(); $handler = opendir($directory); $data = ''; $fp = fopen('data.txt', 'w'); while ($file = readdir($handler)) { if ($file != '.' && $file != '..') { $data .= "<track>"; $data .= "<location>$directory$file</location>"; $data .= "<image></image>"; $data .= "<annotation>$filename</annotation>"; $data .= "</track>"; } } fwrite($fp, $data); fclose($fp); closedir($handler); } // do it list_mp3s(); ?> Link to comment https://forums.phpfreaks.com/topic/139233-could-anyone-help-me-make-this-write-into-a-file/#findComment-728295 Share on other sites More sharing options...
JREAM Posted January 2, 2009 Author Share Posted January 2, 2009 gosh Im such an idiot. thanks fly and mch Link to comment https://forums.phpfreaks.com/topic/139233-could-anyone-help-me-make-this-write-into-a-file/#findComment-728307 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.