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?

 

Link to comment
Share on other sites

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
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.