Всем привет, подскажите пожалуйста как сделать что бы файл загружался на сайт, а имя и путь загруженного файла записывались в базу, для дальнейшего прослушивания на сайте.
Вот код php файла добавления треков по ссылкам
<?php
if (!defined('DATALIFEENGINE')) {die("Hacking attempt!");}
include_once ENGINE_DIR . '/classes/parse.class.php';
$parse = new ParseFilter(Array(), Array(), 1, 1);
if (!$_REQUEST['savemusic']) {
$db->query("SELECT * FROM " . PREFIX . "_music_category");
$category = '<select name="category">';
while ($row = $db->get_row()) {
$catId = $row['id'];
$catName = $row['name'];
$category .= "<option value="$catId">$catName</option>";
}
$category .= '</select>';
$tpl->load_template('music/amusic.tpl');
$tpl->set('{title}', "");
$tpl->set('{description}', "");
$tpl->set('{category}', $category);
$script = "<form method=post name="music-add" id="music-add" action="">";
$tpl->copy_template = $script . $tpl->copy_template . "</form>";
$tpl->compile('content');
$tpl->clear();
}
if ($_REQUEST['savemusic']) {
$stop = "";
$title = $db->safesql($_POST['title']);
if (strlen($title) < 3 OR strlen($title) > 100) $stop .= "Название трека слишком короткое или слишком длинное.<br>";
$description = $db->safesql($parse->process($_POST['description']), false);
$description = preg_split('((>)|(<))', $description, - 1, PREG_SPLIT_DELIM_CAPTURE);
$n = count($description);
for ($i = 0; $i < $n; $i++) {
if ($description[$i] == "<") {
$i++;
continue;
}
$description[$i] = preg_replace("#([^snr]{" . intval(50) . "})#i", "\1", $description[$i]);
}
$description = join("", $description);
if (strlen($description) < 5) $stop .= "Слишком короткая ссылка на аудиозапись.<br>";
if ($stop) {
$stop .= " <a href="javascript:history.go(-1)">$lang[all_prev]</a>";
msgbox($lang['add_err_6'], $stop);
}
else {
$Category = (int) $_REQUEST['category'];
$Category = $Category ? $Category : 0;
$db->query("INSERT INTO " . PREFIX . "_music SET name = '$title', category = '$Category', onserver = '$description', author = '$member_id[name]', date = '$_TIME'");
header( "Location: /music/" );
die();
}
}
?>
<div class="base">
<div class="heading3"><h1>Добавление нового трека</h1></div>
<div class="maincont">
<table class="tableform">
<tr>
<td class="label">Название:</td>
<td><input type="text" name="title" value="{title}" maxlength="150" class="f_input" /></td>
</tr>
<tr>
<td class="label">Категория:</td>
<td>{category}</td>
</tr>
<tr>
<td class="label">Ссылка:</td>
<td><input type="text" name="description" value="{description}" maxlength="150" class="f_input" /></td>
</tr>
</table>
<input name="savemusic" type="submit" class="zp-button" value="Загрузить" />
</div>
<div class="clr"></div>
</div>
$maxsize = 10240000;
$accepted = array('mp3');
$filedir = 'uploads/music';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
preg_match('/.([a-zA-Z]+?)$/', $_FILES['file']['name'], $matches);
if(in_array(strtolower($matches[1]), $accepted)) {
if($_FILES['file']['size'] <= $maxsize) {
$newname = md5_file($_FILES['file']['tmp_name']).'.'.$matches[1];
move_uploaded_file($_FILES['file']['tmp_name'], $filedir.'/'.$newname);
$title = 'http://'.$_SERVER['HTTP_HOST'].preg_replace('//([^/]+?)$/', '/', $_SERVER['PHP_SELF']).'#'.$newname;
$description = 'http://'.$_SERVER['HTTP_HOST'].preg_replace('//([^/]+?)$/', '/', $_SERVER['PHP_SELF']).$filedir.'/'.$newname;
}
else
$stop .= "<p>Извините, максимальный размер файла 10 mb</p>";
}
else
$stop .= "<p>Извините, данный формат файла запрещён</p>";
}
По ссылкам все работает как надо, а загрузка файлов не работает :(
Источник:
dle-faq.pro, DLE