我有一个表单字段,其中包含我要用于课程的所有信息,这些信息会将歌曲上传到目录并将一些数据输入到数据库中。脚本尚未完成,但目前我想测试上传功能。 PHP正在为长度输入字段抛出一个未定义的索引消息。我无法弄清楚出了什么问题!
<?php
require_once('config/config.php');
require('classes/musicClass.php');
$music = new music();
include('views/header.php');
if (isset($_SESSION['user_id']) && $_SESSION['user_id'] == 1) {
?>
<form action="mypage.php" method="post" enctype="multipart/form-data">
<label for="name">Track Name</label>
<input type="text" name="track_name" placeholder="Name of song" id="name" class="text ui-widget-content ui-corner-all">
<label for="name">Artist</label>
<input type="text" name="artist" placeholder="Name of artist" id="artist" class="text ui-widget-content ui-corner-all">
<label for="genre">Genre</label>
<input type="text" name="genre" placeholder="Genre" id="genre" value="" class="text ui-widget-content ui-corner-all">
<label for="subgenre">Sub-Genre</label>
<input type="text" name="sub_genre" placeholder="Sub-Genre" id="subgenre" value="" class="text ui-widget-content ui-corner-all">
<label for="mood">Tag</label>
<input type="text" name="tag" placeholder="Tag" id="tag" value="" class="text ui-widget-content ui-corner-all">
<label for="mood">Tag2</label>
<input type="text" name="tag2" placeholder="Tag 2" id="tag" value="" class="text ui-widget-content ui-corner-all">
<label for="mood">Tag3</label>
<input type="text" name="tag3" placeholder="Tag 3" id="tag" value="" class="text ui-widget-content ui-corner-all">
<label for="length">Full langth?</label><br />
Yes: <input type="radio" name="length" value="Yes"><br />
No: <input type="radio" name="length" value="No"><br />
<label for="description">Description:</label>
<input type="textarea" name="description" placeholder="Type a description of your song" id="description" value="" class="text ui-widget-content ui-corner-all">
<label for="file">File:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="upload_submit" value="Upload" id="file" />
<?php
} else {
echo 'You are not allowed to use this page';
}
?>
<?php require_once('views/footer.php'); ?>
<?php
class music {
public $file_name = '';
public $errors = array();
public $notice = array();
public $db_connect = null;
public function __construct()
{
session_start();
if(isset($_POST['upload_submit'])) {
if(isset($_SESSION['user_id'])) {
if($_SESSION['user_id'] == 1) {
$this->upload_music($_POST['track_name'], $_POST['artist'], $_POST['genre'], $_POST['sub_genre'], $_POST['tag'], $_POST['tag2'], $_POST['tag3'], $_POST['length'], $_POST['description']);
} else {
$this->notice[] = 'You are not authorized to use this feature';
}
} else {
$this->notice[] = 'You must be logged in to upload';
}
}
}
private function database_connection()
{
// if connection already exists
if ($this->db_connect != null) {
return true;
} else {
try {
$this->db_connect = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME . ';charset=utf8', DB_USER, DB_PASS);
return true;
} catch (PDOException $e) {
$this->errors[] = 'There was an error:' . $e->getMessage();
}
}
// default return
return false;
}
public function upload_music($track_name, $artist, $genre, $sub_genre, $tag, $tag2, $tag3, $length, $description)
{
if(empty($track_name)){
$this->errors[] = 'Track name field is empty';
} elseif(empty($artist)){
$this->errors[] = 'Artist field is empty';
} else {
$allowed = array("wav", "mp3", "mp4", "aiff", "m4a");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (in_array($extension, $allowed)){
if ($_FILES["file"]["error"] > 0)
{
$this->errors[] = "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists("C:/Apache24/grant/uploads/" . $_FILES["file"]["name"]))
{
$this->errors[] = $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"C:/Apache24/grant/uploads/" . $_FILES["file"] ["name"]);
$this->notice[] = "Stored in: " . "uploads/" . $_FILES["file"]["name"];
}
}
}
else
{
$this->errors[] = "Invalid file";
}
}
}
}
?>