here is the code
Code: Select all
<!doctype html>
<html>
<head>
<title> upload songs </title>
</head>
<body>
<form action="upload_songs.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
<?php
if (isset($_POST["submit"])) {
$path = './upload/';
$maxFileSize = 1 * (1024 * 1024 * 20); // 20Mb
$allowedExts = array("mp3", "wma", "aif");
$allowedMimes = array("audio/mp3", "audio/mpeg", "audio/x-ms-wma", "audio/x-aiff");
// $extension = end(explode(".", $_FILES["file"]["name"])); this generates warning pathinfo doesn't
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (($_FILES["file"]["size"] < $maxFileSize) && in_array($_FILES["file"]["type"], $allowedMimes) && in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists($path . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], $path . $_FILES["file"]["name"]);
echo "Stored in: $path" . $_FILES["file"]["name"];
}
}
} else {
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Invalid file<br/>";
echo '<pre>' . print_r($_FILES) . '</pre>';
}
}
?>
</body>
</html>