Jump to content

Mp3 Files Into Category Folders


melchedsik

Recommended Posts

I have two php files one with audio categories that allow a music selection when uploading MP3 and the other code is the audio file. I have it when MP3 files are uploaded it goes into a MP3 folder. I have other folders inside the MP3 folder which are the names of the category list (example: country folder, rock folder, etc) how can I get the MP3 files not to just go into the MP3 folder but when they are uploaded and a category selection is made it goes into that direct folder. Below are the codes:

 

 

Audiocat code:

 

<?php

include 'core/init.php';

include 'includes/overall/header.php';

if (isset($_GET['cat']) === true) {

$cat = $_GET['cat'];

 

$audios = all_audio($cat);

 

} else {

header('Location: /');

exit();

}

?>

<h1>Audio</h1>

<p>Viewing category <strong><?php echo audio_category_name_from_id($cat); ?></strong></p>

<?php

if (empty($audios) === true) {

?>

Sorry, there are no audio files in this category

<?php

} else {

foreach($audios as $audio) {

?>

<a href="listen.php?a=<?php echo $audio['audio_id']; ?>"><?php echo $audio['audio_file']; ?></a><br>

<?php

}

}

?>

<?php include 'includes/overall/footer.php'; ?>

 

 

 

Audio code:

 

<?php

include 'core/init.php';

protect_page();

include 'includes/overall/header.php';

if (isset($_GET['delete']) === true) {

$delete = $_GET['delete'];

user_delete_audio($delete);

header('Location: audio.php');

}

?>

<h1>Your audio</h1>

<p>Users can listen to your audio on your profile page</p>

<?php

if (empty($_FILES) === false && isset($_POST['cat']) === true) {

$allowed = array('mp3');

$filename = $_FILES['audio_file']['name'];

$fileext = strtolower(end(explode('.', $filename)));

$tmp = $_FILES['audio_file']['tmp_name'];

 

$cat = $_POST['cat'];

 

if (in_array($fileext, $allowed) === false) {

?>

<p><strong>Sorry, only mp3 files are allowed to be uploaded</strong></p>

<?php

} else if (user_audio_count($_SESSION['user_id']) >= 5) {

?>

<p><strong>Maximum 5 files allowed, sorry.</strong></p>

<?php

} else {

if (cat_exists($cat) === false) {

?>

<p><strong>Something went really wrong here, sorry.</strong></p>

<?php

} else {

user_add_audio($filename . '-' . $_SESSION['user_id'], $cat);

move_uploaded_file($tmp, 'mp3/' . $filename . '-' . $_SESSION['user_id'] . '.mp3');

header('Location: audio.php');

}

}

}

?>

<form action="" method="post" enctype="multipart/form-data">

<p>

Add audio:

<input type="file" name="audio_file">

 

<select name="cat">

<?php

foreach(list_categories() as $cat) {

?>

<option value="<?php echo $cat['cat_id']; ?>"><?php echo $cat['category']; ?></option>

<?php

}

?>

</select>

 

<input type="submit" value="Upload">

</p>

</form>

<?php

$audios = user_audio($_SESSION['user_id']);

if (empty($audios) === true) {

?>

<p>You haven't added any audio files yet.</p>

<?php

} else {

foreach($audios as $audio) {

?>

<a href="listen.php?a=<?php echo $audio['audio_id']; ?>"><?php echo $audio['audio_file']; ?></a> in <?php echo audio_category_name_from_id($audio['cat_id']); ?> (<a href="audio.php?delete=<?php echo $audio['audio_id']; ?>">Delete</a>)<br>

<?php

}

}

include 'includes/overall/footer.php';

?>

Edited by melchedsik
Link to comment
Share on other sites

how can I get the MP3 files not to just go into the MP3 folder but when they are uploaded and a category selection is made it goes into that direct folder.

You'll have to specify the category as part of the path name when moving the uploaded file.

 

How do you think this line should be modified?

move_uploaded_file($tmp, 'mp3/' . $filename . '-' . $_SESSION['user_id'] . '.mp3');

Edited by shlumph
Link to comment
Share on other sites

Can anyone help?

 

 

I know the change has to be here but maybe I am doing something wrong......It took me almost a year just to get this far.

 

move_uploaded_file($tmp, 'mp3/' . $filename . '-' . $_SESSION['user_id'] . '.mp3');

 

Here are the folders inside of the mp3 folder (country, rock, electronic, raggea, world) so when a category selected and the audio(mp3 file) uploads I need for it to go into the selected folder that was selected doing the upload (example: mp3/country/mysong.mp3).

Link to comment
Share on other sites

Here are the folders inside of the mp3 folder (country, rock, electronic, raggea, world) so when a category selected and the audio(mp3 file) uploads I need for it to go into the selected folder that was selected doing the upload (example: mp3/country/mysong.mp3).

Alright, you said it's going into the MP3 folder, right? So all you need to do is include the category folder in the path.

$destination = 'mp3/' . $cat . '/' . $filename . '-' . $_SESSION['user_id'] . '.mp3';

 

You might want to make sure the directory exists, and if it doesn't, create it:

if(!is_dir(dirname($destination))) {
mkdir($destination, 0777, true); //Change 0777 to whatever permission you're comfortable with
}

move_uploaded_file($tmp, $destination);

Edited by shlumph
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.