Question: I am having a hard time with getting my code to write the txt file. Any ideas? Song Organizer Song Organizer Sort Song List Remove

I am having a hard time with getting my code to write the txt file. Any ideas?

Song Organizer

Song Organizer

// create a song organizer script

$filename = 'songs.txt';

$filepath = __DIR__ . '/' . $filename;

$songs = [];

if (isset($_GET['action']))

{

if ((file_exists("SongOrganizer2/songs.txt"))

&& (filesize("SongOrganizer2/songs.txt")!= 0))

{$SongArray = file("SongOrganizer2/songs.txt");

switch ($_GET['action'])

{

case 'Remove Duplicates';

$SongArray = array_unique($SongArray);

$SongArray = array_values($SongArray);

break;

case 'Sort Ascending':

sort($SongArray);

break;

case 'Shuffle':

shuffle($SongArray);

break;

} // End of the switch statement

if (count($SongArray) > 0)

{

$NewSongs = implode($SongArray);

$SongStore = fopen("SongOrganizer2/songs.txt","wb");

if ($SongStore === false)

echo "There was an error updating the song file ";

else

{fwrite($SongStore, $NewSongs);

fclose($SongStore);

}

} else

unlink("SongOrganizer2/songs.txt");

}

}

if(isset($_POST['submit']))

$SongToAdd = stripslashes($_POST['SongName']) . " ";

$ExistingSongs = array();

if (file_exists("SongOrganizer2/songs.txt")

&filesize("SongOrganizer2/songs.txt")> 0)

{ $ExistingSongs = file("SongOrganizer2/songs.txt");}

if (in_array($SongToAdd, $ExistingSongs))

{echo "

The song you entered already exists!
";

echo "Your song was not added to the list.

";

}

else

{

$SongFile = fopen("SongOrganizer2/songs.txt","ab");

if ($SongFile === false)

echo "There was an error saving your message! ";

else

{

fwrite($SongFile, $SongToAdd);

fclose($SongFile);

echo "Your song has been added to the list. ";

}

}

if ((!file_exists("SongOrganizer2/songs.txt"))

|| (filesize("SongOrganizer2/songs.txt")== 0))

echo "

There are no songs in the list.

";

else

{

$SongArray = file("SongOrganizer2/songs.txt");

echo "

";

foreach ($SongArray as $Song) {

echo "

";

echo "

";

echo "

";}

echo "

" . htmlentities($Song) ."
";

}

?>

Sort Song List

Remove Duplicate Songs

Randomize Song List

Add a New Song

Song Name:

value="Add Song to List" />

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!