test.php
unknown
php
2 years ago
18 kB
8
Indexable
Salut! Am acest fisier php 'songs.php'
"<?php
session_start();
$servername = "localhost";
$username = "horiamcc_user";
$password = "Pysquaifk!1@@";
$dbname = "horiamcc_songs";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Eroare de conectare la baza de date: " . $e->getMessage();
exit();
}
// Verificați dacă utilizatorul este autentificat
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
$_SESSION['redirectURL'] = $_SERVER['REQUEST_URI'];
header('Location: login.php');
exit;
}
$userId = $_SESSION['user_id'];
$stmt = $conn->prepare("SELECT * FROM songs WHERE user_id = :user_id");
$stmt->bindParam(':user_id', $userId);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$target_dir = "uploads-songs/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$fileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
if (isset($_POST["submit"])) {
// Aici puteți adăuga propriile condiții de verificare, dacă este necesar
}
if (file_exists($target_file)) {
echo "Fișierul deja există.";
$uploadOk = 0;
}
if ($fileType != "mp3" && $fileType != "wav") {
echo "Doar fișierele MP3 și WAV sunt permise.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Fișierul nu a fost încărcat.";
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "Fișierul " . htmlspecialchars(basename($_FILES["file"]["name"])) . " a fost încărcat.";
// Salvare informații despre fișier în baza de date
$label = $_POST['label'];
$artist = $_POST['artist'];
$tonality = $_POST['tonality'];
$bpm = $_POST['bpm'];
$songName = $_POST['song_name'];
$servername = "localhost";
$username = "horiamcc_user";
$password = "Pysquaifk!1@@";
$dbname = "horiamcc_songs";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Verifică dacă există deja o coloană numită 'song_name' în tabelul 'songs'
$checkColumnExists = $conn->query("SHOW COLUMNS FROM songs LIKE 'song_name'");
if ($checkColumnExists->rowCount() == 0) {
$conn->exec("ALTER TABLE songs ADD COLUMN song_name VARCHAR(255) AFTER bpm");
}
$sql = "INSERT INTO songs (user_id, label, artist, tonality, bpm, file, song_name)
VALUES ('$userId', '$label', '$artist', '$tonality', '$bpm', '$target_file', '$songName')";
$conn->exec($sql);
echo "Noua înregistrare a fost adăugată cu succes!";
} catch (PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
} else {
echo "A existat o eroare la încărcarea fișierului.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload Pagina</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
padding: 50px;
margin: 0;
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.form-container {
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 30px 0 rgba(0, 0, 0, 0.1);
width: 500px;
transition: all 0.3s ease;
margin-bottom: 20px;
}
form {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
color: #333;
font-size: 14px;
}
input, select {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border-radius: 5px;
border: 1px solid #ccc;
font-size: 16px;
}
.hide {
display: none;
}
input[type="submit"] {
background-color: #007BFF;
color: white;
cursor: pointer;
border: none;
font-size: 16px;
transition: all 0.3s ease;
}
input[type="submit"]:hover {
background-color: #0056b3;
}
.uploaded-files {
margin-top: 50px;
}
.uploaded-file {
background-color: #fff;
padding: 20px;
margin-bottom: 20px;
border-radius: 10px;
box-shadow: 0 0 30px 0 rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
}
.uploaded-file:hover {
box-shadow: 0 0 40px 4px rgba(0, 0, 0, 0.1);
}
.uploaded-file h3 {
margin: 0;
font-size: 18px;
}
.uploaded-file p {
margin: 5px 0;
font-size: 16px;
}
.uploaded-file a {
color: #007BFF;
text-decoration: none;
}
.uploaded-file a:hover {
color: #0056b3;
}
.section-title {
margin-top: 30px;
margin-bottom: 20px;
font-size: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<div class="form-container">
<form action="upload-song.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="user_id" value="<?php echo $userId; ?>">
<label for="file">Alege un fișier:</label>
<input type="file" name="file" id="file" accept=".mp3,.wav">
<label for="song_name">Titlu melodie:</label>
<input type="text" name="song_name" id="song_name">
<label for="label">Label:</label>
<select name="label" id="label" class="form-select" required onchange="setArtists(this.value)">
<option value="">Alege un label</option>
<option value="Xtra Music Media">Xtra Music Media</option>
<option value="The One Records">The One Records</option>
<option value="Altul">Altul</option>
</select>
<div id="artist_select">
<label for="artist">Artist:</label>
<select name="artist" id="artist" class="form-select" required>
</select>
</div>
<div id="new_artist_input" class="hide">
<label for="new_artist">Numele noului artist:</label>
<input type="text" name="new_artist" id="new_artist">
</div>
<label for="tonality" class="form-label">Tonalitate:</label>
<select name="tonality" id="tonality" class="form-select" required>
<option value="">Alege o tonalitate</option>
<optgroup label="Tonalități majore">
<option value="C Major">C Major</option>
<option value="C# Major">C# Major</option>
<option value="D Major">D Major</option>
<option value="D# Major">D# Major</option>
<option value="E Major">E Major</option>
<option value="F Major">F Major</option>
<option value="F# Major">F# Major</option>
<option value="G Major">G Major</option>
<option value="G# Major">G# Major</option>
<option value="A Major">A Major</option>
<option value="A# Major">A# Major</option>
<option value="B Major">B Major</option>
</optgroup>
<optgroup label="Tonalități minore">
<option value="C Minor">C Minor</option>
<option value="C# Minor">C# Minor</option>
<option value="D Minor">D Minor</option>
<option value="D# Minor">D# Minor</option>
<option value="E Minor">E Minor</option>
<option value="F Minor">F Minor</option>
<option value="F# Minor">F# Minor</option>
<option value="G Minor">G Minor</option>
<option value="G# Minor">G# Minor</option>
<option value="A Minor">A Minor</option>
<option value="A# Minor">A# Minor</option>
<option value="B Minor">B Minor</option>
</optgroup>
</select>
<label for="bpm">BPM:</label>
<input type="number" name="bpm" id="bpm">
<input type="submit" value="Încarcă fișier" name="submit">
</form>
<div class="uploaded-files">
<?php
// Conectează la baza de date
$mysqli = new mysqli("localhost", "horiamcc_user", "Pysquaifk!1@@", "horiamcc_songs");
// Verifică conexiunea
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Selectează toate înregistrările din tabelul `songs`
$sql = "SELECT * FROM songs WHERE user_id = '$userId'";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
$xtraMusicMediaSongs = [];
$theOneRecordsSongs = [];
$altulSongs = [];
// Separă melodiile în funcție de label
while($row = $result->fetch_assoc()) {
if ($row['label'] === 'Xtra Music Media') {
$xtraMusicMediaSongs[] = $row;
} elseif ($row['label'] === 'The One Records') {
$theOneRecordsSongs[] = $row;
} else {
$altulSongs[] = $row;
}
}
// Afiseaza melodiile pentru Xtra Music Media
if (!empty($xtraMusicMediaSongs)) {
echo "<div class='section-title'>Xtra Music Media</div>";
foreach ($xtraMusicMediaSongs as $song) {
echo "<div class='uploaded-file'>
<h3>".htmlspecialchars($song['title'])."</h3>
<p><strong>Label:</strong> ".htmlspecialchars($song['label'])."</p>
<p><strong>Artist:</strong> ".htmlspecialchars($song['artist'])."</p>
<p><strong>Nume melodie:</strong> ".htmlspecialchars($song['song_name'])."</p>
<p><strong>Tonalitate:</strong> ".htmlspecialchars($song['tonality'])."</p>
<p><strong>BPM:</strong> ".htmlspecialchars($song['bpm'])."</p>
<p><strong>Fișier:</strong> <a href='uploads-songs/".htmlspecialchars($song['file'])."' target='_blank'>Deschide fișier</a></p>
</div>";
}
}
// Afiseaza melodiile pentru The One Records
if (!empty($theOneRecordsSongs)) {
echo "<div class='section-title'>The One Records</div>";
foreach ($theOneRecordsSongs as $song) {
echo "<div class='uploaded-file'>
<h3>".htmlspecialchars($song['title'])."</h3>
<p><strong>Label:</strong> ".htmlspecialchars($song['label'])."</p>
<p><strong>Artist:</strong> ".htmlspecialchars($song['artist'])."</p>
<p><strong>Nume melodie:</strong> ".htmlspecialchars($song['song_name'])."</p>
<p><strong>Tonalitate:</strong> ".htmlspecialchars($song['tonality'])."</p>
<p><strong>BPM:</strong> ".htmlspecialchars($song['bpm'])."</p>
<p><strong>Fișier:</strong> <a href='uploads-songs/".htmlspecialchars($song['file'])."' target='_blank'>Deschide fișier</a></p>
</div>";
}
}
// Afiseaza melodiile pentru Altul
if (!empty($altulSongs)) {
echo "<div class='section-title'>Altul</div>";
foreach ($altulSongs as $song) {
echo "<div class='uploaded-file'>
<h3>".htmlspecialchars($song['title'])."</h3>
<p><strong>Label:</strong> ".htmlspecialchars($song['label'])."</p>
<p><strong>Artist:</strong> ".htmlspecialchars($song['artist'])."</p>
<p><strong>Nume melodie:</strong> ".htmlspecialchars($song['song_name'])."</p>
<p><strong>Tonalitate:</strong> ".htmlspecialchars($song['tonality'])."</p>
<p><strong>BPM:</strong> ".htmlspecialchars($song['bpm'])."</p>
<p><strong>Fișier:</strong> <a href='uploads-songs/".htmlspecialchars($song['file'])."' target='_blank'>Deschide fișier</a></p>
</div>";
}
}
} else {
echo "Nu există fișiere încărcate.";
}
$mysqli->close();
?>
</div>
</div>
</div>
<script>
var artists = {
'Xtra Music Media': ['Chriss', 'Karina Maria', 'Iris Ciocaniu'],
'The One Records': ['Andreea Bostanica', 'Denisa Avram (Bubblezz)', 'Christinne', 'Cristian Porcari', 'Dael Damsa', 'Daniela Matei', 'Dominique', 'Tan Feelz']
};
function setArtists(label) {
var artistSelect = document.getElementById('artist_select');
var newArtistInput = document.getElementById('new_artist_input');
var artistSelectBox = document.getElementById('artist');
artistSelectBox.innerHTML = '';
if (label === 'Altul') {
newArtistInput.classList.remove('hide');
artistSelect.classList.add('hide');
} else {
newArtistInput.classList.add('hide');
artistSelect.classList.remove('hide');
if (artists[label]) {
artists[label].forEach(function(artist) {
var opt = document.createElement('option');
opt.value = artist;
opt.innerHTML = artist;
artistSelectBox.appendChild(opt);
});
}
}
}
</script>
</body>
</html>
"
si acest fisier 'upload-song.php'
"<?php
$target_dir = "uploads-songs/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$fileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
if (isset($_POST["submit"])) {
// Aici puteți adăuga propriile condiții de verificare, dacă este necesar
}
if (file_exists($target_file)) {
echo "Fișierul deja există.";
$uploadOk = 0;
}
if ($fileType != "mp3" && $fileType != "wav") {
echo "Doar fișierele MP3 și WAV sunt permise.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Fișierul nu a fost încărcat.";
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "Fișierul " . htmlspecialchars(basename($_FILES["file"]["name"])) . " a fost încărcat.";
} else {
echo "A existat o eroare la încărcarea fișierului.";
}
}
$label = $_POST['label'];
$artist = $_POST['artist'];
$tonality = $_POST['tonality'];
$bpm = $_POST['bpm'];
$songName = $_POST['song_name'];
$userId = $_SESSION['user_id'];
$servername = "localhost";
$username = "horiamcc_user";
$password = "Pysquaifk!1@@";
$dbname = "horiamcc_songs";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Verifică dacă există deja o coloană numită 'song_name' în tabelul 'songs'
$checkColumnExists = $conn->query("SHOW COLUMNS FROM songs LIKE 'song_name'");
if ($checkColumnExists->rowCount() == 0) {
$conn->exec("ALTER TABLE songs ADD COLUMN song_name VARCHAR(255) AFTER bpm");
}
$sql = "INSERT INTO songs (user_id, label, artist, tonality, bpm, file, song_name)
VALUES ('$userId', '$label', '$artist', '$tonality', '$bpm', '$target_file', '$songName')";
$conn->exec($sql);
echo "Noua înregistrare a fost adăugată cu succes!";
} catch (PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
"
Si am nevoie ca fiecare utilizator autentificat sa poata sa uploadeze fisiere mp3 / wav , completand formularul, si dupa fiecare utilizator sa isi vada propriile fisiere incarcate alaturi de datele respective in chenare.Editor is loading...