melchedsik Posted October 26, 2012 Share Posted October 26, 2012 (edited) 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 October 26, 2012 by melchedsik Quote Link to comment https://forums.phpfreaks.com/topic/269946-mp3-files-into-category-folders/ Share on other sites More sharing options...
shlumph Posted October 26, 2012 Share Posted October 26, 2012 (edited) 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 October 26, 2012 by shlumph Quote Link to comment https://forums.phpfreaks.com/topic/269946-mp3-files-into-category-folders/#findComment-1387981 Share on other sites More sharing options...
melchedsik Posted October 26, 2012 Author Share Posted October 26, 2012 If you're taalking about added the folder names after the mp3/ it didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/269946-mp3-files-into-category-folders/#findComment-1387982 Share on other sites More sharing options...
melchedsik Posted October 26, 2012 Author Share Posted October 26, 2012 I even tried to array them. Quote Link to comment https://forums.phpfreaks.com/topic/269946-mp3-files-into-category-folders/#findComment-1387983 Share on other sites More sharing options...
melchedsik Posted October 26, 2012 Author Share Posted October 26, 2012 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). Quote Link to comment https://forums.phpfreaks.com/topic/269946-mp3-files-into-category-folders/#findComment-1388010 Share on other sites More sharing options...
shlumph Posted October 29, 2012 Share Posted October 29, 2012 (edited) 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 October 29, 2012 by shlumph Quote Link to comment https://forums.phpfreaks.com/topic/269946-mp3-files-into-category-folders/#findComment-1388445 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.