合理配置php.ini
如何配置PHP.ini实现PHP文件上传功能。其中涉及到PHP.ini配置文化中的upload_tmp_dir、upload_max_filesize、post_max_size等
file_upload=on
是否允许HTTP文件上传。默认值为on允许HTTP文件上传,此选项不能设置为off
upload_tmp_dir=
文件上传的临时存放目录。如果没指定则PHP会使用系统默认的临时目录。如果不配置这个选项,文件上传就无法实现,必须给这个选项赋值,比如upload_tmp_dir=”d:/wamp/tmp”,代表在D盘下的wamp目录下有一个tmp目录,并且给这个目录赋予所有用户读写权限。
upload_max_filesize=2M
上传文件的最大尺寸。这个选项默认值为2M。如果想上传一个50M的文件,必须设定upload_max_filesize=50M。但是仅设置upload_max_filesize=50M还是无法实现大文件的上传功能,相应的还需要修改post_max_size选项。
post_max_size=8M
指通过表单post给PHP的所能接收的最大值,包括表单里的所有值。默认为8M。如果post数据超出限制,那么
POST和
_FILE将会为空。
要上传大文件,必须设定选项值大于upload_max_filesize选项的值。另外如果启用了内存限制,那么该值应当小于memory_limit选项的值。
max_file_uploads=20
设置一次最多允许上传文件的数量。
max_execution_time=30
每个PHP页面运行的最大时间值(单位秒),默认30秒。当上传一个较大的文化,例如50M的文件,很有可能几分钟才能上传完,因此应该把此值设置较大一些,如max_execution_time=600.如果设置为0,则表示无时间限制。
max_input_time=60
每个PHP脚本解析请求数据所用的时间(单位秒),默认60秒。当上传大文件时,可以将这个值设置较大些。如果设置为0,则表示无时间限制。
memory_limit=128M
这个选项用来设置单个PHP脚本所能申请到的最大内存空间。这有助于防止写的不好的脚本消耗光服务器上的可用内存。如果不需要任何内存上的限制将其设置为-1.
总结一下上传中的逻辑判断
1. 先判断是否上传文件
2. 如果有再来判断上传中是否出错
3. 如果有错,则提示出错信息
4. 如果没有错,再判断文件类型
5. 如果类型符合条件,再判断指定目录中有没有存在该文件
6. 如果没有就把该文件移到指定目录
上传文件先创建目录,再上传到目录里面去并建立数据库的文件地址链接
HTML代码:
<?php
/**
* Created by PhpStorm.
* User: hongqiang
* Date: 2017/1/2
* Time: 19:36
*/
session_start();
if ($_SESSION['permission'] == null){
header("Location::error.html");
exit;
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title></title>
</head>
<body>
<div align="center">
<br />
<div>
<br />
<font face="Georgia, Times New Roman, Times, serif" size="3" color="#A4B8F2">You are here: Add new File</font>
</div>
<br />
<hr width="50%" id="hr_home"/>
<br /><br />
<form action="upload.php" method="post" enctype="multipart/form-data">
<div align="center">
<table border="0" cellpadding="5px" cellspacing="5px" width="50%">
<tr>
<td width="40%" align="right">
<p>
File Location: </td>
<td width="60%" align="left">
<input type="file" name="file" id="form2" />
</td>
</p></tr>
<tr>
<td width="40%" align="right">
<p>
File Owner: </td>
<td width="60%" align="left">
<input type="text" name="owner" id="form2" size="34" />
</td>
</p></tr>
<tr>
<td width="40%" align="right">
<p>
Upload Time:
</td>
<td width="60%" align="left">
<input type="date" name="time" id="form2" />
</td>
</p></tr>
<tr>
<td width="40%" align="right">
<p>
File Classification:
</td>
<td width="60%" align="left">
<select name="classification" id="form2">
<option value="ecg" selected> 心 电 </option>
<option value="afib"> 房 颤 </option>
<option value="tumble"> 跌 倒 </option>
</select></td>
</p></tr>
<tr>
<td width="40%" align="right">
<p>
File Discription:
</td>
<td width="60%" align="left">
<textarea cols="30" rows="5" name="discription" id="form2"></textarea>
</td>
</p>
</tr>
</table>
<br />
<input type="submit" value="INSERT" id="form2" />
</div>
</form>
</div>
</body>
</html>
PHP代码:
<?php
/**
* Created by PhpStorm.
* User: hongqiang
* Date: 2017/1/3
* Time: 10:56
*/
session_start();
include ("connect_db.php");
if ($_SESSION['permission'] == null){
header("Location::error.html");
exit;
}
$file_owner = $_POST['owner'];
$file_time = $_POST['time'];
$file_classification = $_POST['classification'];
$file_discription = $_POST['discription'];
$path = ""; //定义存储路径
$file_dir = './upload/'.$file_classification.'/'; //最终保存目录
if (!is_dir($file_dir)){
$mkdir_file_dir = mkdir('./upload/'.$file_classification, 0777); //在upload下建立一个文件夹,并设定文件夹的权限
$file_dir = './upload/'.$file_classification.'/';
}
if ($_FILES['file']['name'] != ''){
if ($_FILES['file']['error'] > 0){
switch ($_FILES['file']['error']){
case 1:
echo "文件大小超过PHP.ini中的文件限制!";
break;
case 2:
echo "文件大小超过了浏览器限制!";
break;
case 3:
echo "文件部分被上传!";
break;
case 4:
echo "没有找到要上传的文件!";
break;
case 5:
echo "服务器临时文件夹丢失,请重新上传!";
break;
case 6:
echo "文件写入到临时文件夹出错!";
break;
}
}else{
$_FILES['file']['name'] = date('YmdHis', time()).$_FILES['file']['name']; //为文件重新命名,避免重复
if (!file_exists($file_dir.$_FILES['file']['name'])){
move_uploaded_file($_FILES['file']['tmp_name'], $file_dir.$_FILES['file']['name']);
$path = $file_dir.$_FILES['file']['name'];
//将文件地址保存到数据库中
$conn = db_connect();
$conn -> query("set names gbk");
$result = $conn -> query("insert into upfiles(file_path, file_owner, file_time, file_classification, file_discription)
VALUES ('".$path."', '".$file_owner."', '".$file_time."', '".$file_classification."',
'".$file_discription."')");
if ($result){
echo "<script>alert(\"上传成功!\");</script>";
}
}else{
echo "<script>alert(\"您上传的文件已经存在!\");</script>";
}
}
}else{
echo "<script>alert(\"请上传文件!\");</script>";
}
header("Location:add.php");
?