php 上传文件文件
【摘要】 Php上传文件方法 前台页面:<form action="../control/filecontrol.php" method="post" enctype="multipart/form-data"><table border="1" width="100%"> <tr> <td>选择图片</td> <td><input type="file" name="file_f...
-
Php上传文件方法
-
-
前台页面:
-
<form action="../control/filecontrol.php" method="post" enctype="multipart/form-data">
-
<table border="1" width="100%">
-
<tr>
-
<td>选择图片</td>
-
<td><input type="file" name="file_file"/></td>
-
</tr>
-
<tr>
-
<td>图片名称</td>
-
<td><input type="text" name="down_name"/></td>
-
</tr>
-
<tr>
-
<td colspan="2"><input type="submit" value="保存文件" /></td>
-
</tr>
-
</table>
-
</form>
-
-
上传操作页面:
-
if(isset ( $_FILES ["file_file"] )){
-
$file = @$_FILES['file_file'];
-
-
//上传验证
-
if ($file["error"] > 0) {
-
echo "上传错误!";
-
exit;
-
}
-
-
//对文件大小对比1000KB以下的图片可以上传
-
$fileSize = sprintf("%.2f",($file["size"] / 1024 / 1024)); //M
-
if($fileSize > 100){
-
echo "文件太大超过100M不能上传!";
-
exit;
-
}
-
-
//文件名称
-
$down_name = $file['name'];
-
$down_urlname = StackConst::get_date_str().substr($down_name,strrpos($down_name,"."),strlen($down_name));
-
-
//从缓存区移动文件
-
if(move_uploaded_file($file["tmp_name"],StackConst::res_file_url().$down_urlname)){
-
-
-
//装填信息
-
$arrFileInfo = array(
-
"down_name"=>@$_POST['down_name'],
-
"down_urlname"=>$down_urlname,
-
"down_savedate"=>StackConst::get_date()
-
);
-
-
//引入FileDao
-
require_once '../dao/FileDao.php';
-
//实例化FileDao
-
$fileDao = new FileDao();
-
-
if($fileDao->addFileSource($arrFileInfo)){
-
echo "文件上传成功!";
-
StackConst::jump_page("../view/filelist.php");
-
}else{
-
echo "文件上传失败!";
-
}
-
}else{
-
echo "文件上传失败!";
-
}
-
-
}
-
-
其中这里包括了把上传文件的信息写入数据库
-
-
FileDao.php
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
<?php
-
header ( 'Content-type: text/html; charset=utf-8' );
-
ini_set("error_reporting","E_ALL & ~E_NOTICE");
-
-
/********************************************************************
-
* 文件操作DAO
-
* add by fuzhengwei
-
* 2013年11月17日 09:38:00
-
********************************************************************/
-
class FileDao{
-
-
/**
-
* 获取文件集合
-
*
-
*/
-
public function getFileList(){
-
-
//引入数据库操作类
-
require_once '../../conndb/mysql/ConnMysqlClass.php';
-
-
//链接数据库
-
$conn = ConnMysqlClass::getConnMysql();
-
mysql_select_db(ConnMysqlClass::getDBName(), $conn);
-
-
$str_sql = "select down_id,down_name,down_urlname,down_savedate,down_statue from rea1_resource_down order by down_id desc";
-
-
$result = mysql_query($str_sql);
-
-
$arrFiles = array();
-
$var = 0;
-
-
while($row = mysql_fetch_array($result)){
-
$arrFiles[$var++] = array(
-
"down_id"=>$row['down_id'],
-
"down_name"=>$row['down_name'],
-
"down_urlname"=>$row['down_urlname'],
-
"down_savedate"=>$row['down_savedate'],
-
"down_statue"=>$row['down_statue']
-
);
-
}
-
-
mysql_close($conn);
-
-
return $arrFiles;
-
}
-
-
/**
-
* 根据id删除下载资源文件
-
* @param 文件id $down_id
-
* @return true/false
-
*/
-
public function deleteFileById($down_id){
-
//引入数据库操作类
-
require_once '../../conndb/mysql/ConnMysqlClass.php';
-
-
//链接数据库
-
$conn = ConnMysqlClass::getConnMysql();
-
mysql_select_db(ConnMysqlClass::getDBName(), $conn);
-
-
$str_sql = "delete from rea1_resource_down where down_id = $down_id";
-
-
$myq = mysql_query($str_sql,$conn);
-
-
mysql_close($conn);
-
-
return $myq;
-
-
}
-
-
/**
-
* 添加文件资源到数据库
-
* @param 文件资源信息 $arrFileInfo
-
*/
-
public function addFileSource($arrFileInfo){
-
-
//引入数据库操作类
-
require_once '../../conndb/mysql/ConnMysqlClass.php';
-
-
//链接数据库
-
$conn = ConnMysqlClass::getConnMysql();
-
mysql_select_db(ConnMysqlClass::getDBName(), $conn);
-
-
$down_name = $arrFileInfo['down_name'];
-
$down_urlname = $arrFileInfo['down_urlname'];
-
$down_savedate = $arrFileInfo['down_savedate'];
-
-
$str_sql = "insert into rea1_resource_down(down_name,down_urlname,down_savedate) values('$down_name','$down_urlname','$down_savedate')";
-
-
$myq = mysql_query($str_sql,$conn);
-
-
mysql_close($conn);
-
-
return $myq;
-
-
}
-
-
}
-
-
-
?>
-
-
还有个是:【获取各种类型的时间串,上传里面用到了】
-
<?php
-
//关闭异常报错
-
ini_set("error_reporting","E_ALL & ~E_NOTICE");
-
//设置时间格式
-
date_default_timezone_set('Asia/Shanghai');
-
-
class StackConst {
-
-
/**
-
* @return Ymd 20131118
-
*
-
*/
-
static public function get_date_min(){
-
//获取时间
-
$dateTime = date('Ymd');
-
//返回系统时间
-
return $dateTime;
-
}
-
-
/**
-
* @return Y-m-d
-
*/
-
static public function get_date_sort(){
-
//获取时间
-
$dateTime = date('Y-m-d');
-
//返回系统时间
-
return $dateTime;
-
}
-
/**
-
* @return Y-m-dH:i:s
-
*/
-
static public function get_date(){
-
//获取时间
-
$dateTime = date('Y-m-d H:i:s');
-
//返回系统时间
-
return $dateTime;
-
}
-
-
/**
-
* @return 获取时间戳
-
*/
-
static public function get_date_str(){
-
-
//获取时间
-
$dateTime = date('Y-m-d H:i:s');
-
-
//获得时间串
-
$year=((int)substr($dateTime,0,4));//取得年份
-
$month=((int)substr($dateTime,5,2));//取得月份
-
$day=((int)substr($dateTime,8,2));//取得几号
-
$second = ((int)substr($dateTime,11,2));//取得几号
-
$minute = ((int)substr($dateTime,14,2));//取得几号
-
$hour = ((int)substr($dateTime,17,2));//取得几号
-
-
//返回时间戳
-
return mktime($hour,$minute,$second,$month,$day,$year);
-
-
}
-
-
/**
-
* @return 图片路径
-
*/
-
static public function res_pic_url(){
-
return "../picstack/";
-
}
-
-
/**
-
* @return 文件路径
-
*/
-
static public function res_file_url(){
-
return "../filestack/";
-
}
-
-
/**
-
* @param 跳转 $url
-
*/
-
static public function jump_page($url){
-
echo "<script language='javascript' type='text/javascript'>";
-
echo "window.location.href='$url'";
-
echo "</script>";
-
}
-
-
}
-
-
?>
文章来源: bugstack.blog.csdn.net,作者:小傅哥,版权归原作者所有,如需转载,请联系作者。
原文链接:bugstack.blog.csdn.net/article/details/17258761
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)