forked from fudansswebfundamental/fdu-17ss-web-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab11.php
74 lines (68 loc) · 2.42 KB
/
lab11.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
if($_SERVER["REQUEST_METHOD"] == 'GET'){
}else if($_SERVER["REQUEST_METHOD"] == 'POST'){
upload();
}else{
exit("<strong>非法访问</strong>");
}
function upload(){
uploadFile();
$lyric = $_POST['lyric'];
$title = $_POST['title'];
$artist = $_POST['artist'];
$txt = "[ti:$title]\n[ar:$artist]\n".$lyric;
$path = "upload/".$artist." - ".$title .".lrc";
$myfile = fopen(iconv ( 'UTF-8', 'GBK', $path ),"w");//编码转换
fwrite($myfile, $txt);
fclose($myfile);
}
function uploadFile(){
$upFile = array();
$fileName = $_FILES['file']['name'];
$fileType = $_FILES['file']['type'];
//获取临时文件的文件名
$fileTemp = $_FILES['file']['tmp_name'];
if($fileName != "" and $fileTemp != "" and $fileType != ""){
if(allowType($fileType)){
//获取文件大小
$upFile['fileSize'] = $_FILES['file']['size'];
//创建文件夹,将上传的文件保存到新创建的文件夹中
$filePath = "upload/"; //设置文件路径
//获取文件扩展名
$fileExtendedName = getExtendedName($fileName);
//设置新的文件名称,以保证上传的文件不重名
$newFileName = $fileName;
//使用move_uploaded_file()函数,将上传的临时文件保存到服务器指定的路径返回状态
$upFile["fileName"] = $newFileName;
$upFile["fileType"] = $fileType;
if(file_exists($filePath . $newFileName)){
unlink($filePath . $newFileName);
}
$upFile["filestat"] = @move_uploaded_file($fileTemp, iconv ( 'UTF-8', 'GBK', $filePath . $newFileName )) ? "true" : "false";
header("Location:lab11.html");
}else{
$upFile["fileName"] = "非法的文件类型。";
$upFile["filestat"] = "false";
}
}else{
$upFile["fileName"] = "无效的文件数据。";
$upFile["filestat"] = "false";
}
return $upFile;
}
//获取文件后缀名
function getExtendedName($fileName){
$tmp = explode(".",$fileName);
return end($tmp);
}
//文件类型验证
function allowType($type){
//设置不允许上传的文件类型数组
$types = array('application/x-js','application/octet-stream','application/x-php','text/html');
if(in_array($type,$types)){
return false;
}else{
return true;
}
}
?>