Jump to content

Could anyone help me make this write into a file?


JREAM

Recommended Posts

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?

 

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);

<?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();
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.