<!DOCTYPE html>
<html> <head> <title>form 表单上传文件</title> <script src="Public/js/jquery-2.1.4.min.js"></script> <script src="Public/js/bootstrap.min.js"></script> </head><body>
<form action="http://www.111.com/renrence3/renrence/lingyuan/Index/form" method="post" enctype="multipart/form-data" name="form1" id="form1"> <label for="name">name</label> <input type="text" name="name" id="name" /> <input type="text" name="projectid" id="projectid" value='1872'> <label for="file">file</label> <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> <!-- 10MB --> <input type="file" name="file" id="file" /> <input type="submit" value="提交"/> </form></body>
</html>
PHP
public function form(){
$projectid=I('projectid'); //将文件放到指定位置 $file_arr=$this->upload($projectid);//将上传的图片放到指定文件夹下 $file_arr['file'][$a][savename]; $data['file_name']=date('Y-m-d').$file_arr['file']['savename'];//获取存入服务器端文件名 //存入数据库 $Model=M('_file'); $data['projectid']=$projectid; $id=$Model->add($data); echo $id; } function upload($projectid) { //完成与thinkphp相关的,文件上传类的调用 // import('@.Org.UploadFile');//将上传类UploadFile.class.php拷到Lib/Org文件夹下 date_default_timezone_set('Asia/Shanghai'); $config = array( 'rootPath' => './', 'maxSize' => 53145728,//限制为3M 'savePath' => 'Public/file/'.$projectid.'/',//保存路径 'saveName' => '/'.date('ymdhis',time()).rand(100,999), 'exts' => array('xlsx', 'txt', 'xls', 'png', 'jpg', 'jpeg', 'ppt', 'docx', 'doc','apk','ipa','pdf','exe'),//限制后罪名 'autoSub' => true,//自动子目录保存Y-M-D ); $upload = new \Think\Upload($config);// 实例化上传类 // // 设置附件上传目录 $info = $upload->upload();return $info; }