index.php
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>TXT文件上传</title>
<link rel="stylesheet" href="./css/updo.css">
</head>
<body>
<div class="uli">
<form class="" id="aa" name="aaa" action="platform.php" method="post" enctype="multipart/form-data">
<div class="ulin">
<label>文件封面:</label>
<div class="ulin_r">
<input class="file" type="file" name="filepicture" id="filepicture">
</div>
</div>
<div class="ulin">
<label>文件名:</label>
<div class="ulin_r">
<input type="text" class="form_input" name="filename" id="filename" placeholder="请输入文件名" />
</div>
</div>
<div class="ulin">
<label>文件文本:</label>
<div class="ulin_r">
<input class="file" type="file" name="filetext" id="filetext">
</div>
</div>
<div class="ulin">
<label>文件详细:</label>
<div class="ulin_r">
<textarea class="form_input" name="filecontent" id="filecontent" rows="8" cols="80"></textarea>
</div>
</div>
<div class="ulin_b">
<input type="submit" name="submit" value="上传文件" style="cursor:pointer">
</div>
</form>
</div>
</body>
</html>
platform.php
<?php
function uploadFile(){
if (!empty($_FILES['filepicture']['name'])) {
$ext = pathinfo($_FILES['filepicture']['name'], PATHINFO_EXTENSION);
// => jpg
$target = './uploads/filepicture-' . uniqid() . '.' . $ext;
if (!move_uploaded_file($_FILES['filepicture']['tmp_name'], $target)) {
echo "<script>alert('上传封面失败');history.go(-1);location.reload();</script>";
return;
}
$filepicture = substr($target, 2);
}else{
echo "<script>alert('请上传封面');history.go(-1);location.reload();</script>";
return;
}
if (empty($_POST['filename'])) {
echo "<script>alert('请输入文件名');history.go(-1);location.reload();</script>";
}
if (!empty($_FILES['filetext']['name'])) {
if (!$_FILES['filetext']['tmp_name']||!$_FILES['filetext']['name']) {
echo "<script>alert('请选择上传的文件!');history.go(-1);location.reload();</script>";
exit();
}
$fileName = basename($_FILES['filetext']['name']);
$tempName = $_FILES['filetext']['tmp_name'];
$date = date("Ymd",time());
$dir = "./uploads/".$date;
// chmod($dir,0777);
if (!is_dir($dir)) {
mkdir($dir,0777,true);
}
$newFile = $dir."/".$fileName;
if (is_uploaded_file($_FILES['filetext']['tmp_name'])) {
$res = move_uploaded_file($_FILES['filetext']['tmp_name'],iconv("gb2312","UTF-8",$newFile));
if (!$res) {
echo "<script>alret('上传失败');history.go(-1);location.reload();</script>";
}
}
$filetext = substr($newFile, 2);
}
if (empty($_POST['filecontent'])) {
echo "<script>alert('请输入文件详细');history.go(-1);location.reload();</script>";
}
$filename = $_POST['filename'];
$filecontent = $_POST['filecontent'];
include './conn/conn.php';
// 2. 开始查询
$query = mysqli_query($conn, "insert into tb_fileuplod values (null, '{$filepicture}', '{$filename}', '{$filetext}', '{$filecontent}');");
if (!$query) {
echo "<script>alert('查询过程失败');history.go(-1);location.reload();</script>";
return;
}
$affected_rows = mysqli_affected_rows($conn);
if ($affected_rows !== 1) {
echo "<script>alert('添加数据失败');history.go(-1);location.reload();</script>";
return;
}else {
echo "<script>alert('添加数据成功');history.go(-1);location.reload();</script>";
}
}
if (isset($_POST['submit'])) {
uploadFile();
}
?>
CSS样式文件updo.css
|