ArticleController.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Admin\Controller;
  3. use Think\Controller;
  4. use Think\Upload;
  5. class ArticleController extends Controller
  6. {
  7. public function lst()
  8. {
  9. $first = D('article');
  10. $firsts = $first->select();
  11. $this->assign('firsts', $firsts);
  12. $this->display();
  13. }
  14. public function add()
  15. {
  16. $first = D('article');
  17. if (IS_POST) {
  18. $data = I("post.");
  19. $data['time'] = time();
  20. if ($_FILES['file']['tmp_name']!=''){
  21. $upload=new Upload();//实例化上传类
  22. $upload->maxSize= 553145728 ;//设置上传大小
  23. $upload->exts=array('png','jpg','gif','jpeg');//设置文件上传类型
  24. $upload->savePath = '/./Uploads/'; // 设置附件上传(子)目录
  25. $upload->rootPath='./';//上传根目录
  26. $upload->saveName=array('uniqid','');//上传文件的保存规则,支持数组和字符串方式定义
  27. $info=$upload->uploadOne($_FILES['file']);
  28. if (!$info){
  29. $this->error($upload->getError());//上传错误提示信息
  30. }else{
  31. $data['src']=$info['savepath'].$info['savename'];
  32. $data['name']=$_FILES['file']['name'];
  33. $data['rname']=$info['savename'];
  34. }
  35. }
  36. if ($first->create($data)) {
  37. if ($first->add()) {
  38. $this->success('新增文章成功', U('Article/lst'));
  39. } else {
  40. $this->error('新增文章失败');
  41. }
  42. } else {
  43. $this->error($first->getError());
  44. }
  45. return;
  46. }
  47. $this->display();
  48. }
  49. public function del()
  50. {
  51. $first = D('article');
  52. if ($first->delete(I('id'))) {
  53. $this->success("删除文章成功", U('Article/lst'));
  54. } else {
  55. $this->error('删除文章失败');
  56. }
  57. }
  58. public function edit(){
  59. header('Content-Type: text/html; charset=utf-8');
  60. $first=D('article');
  61. if(IS_POST){
  62. $data=I('post.');
  63. $data['id']=I('get.id');
  64. $data['time']=time();
  65. if ($_FILES['file']['tmp_name']!=''){
  66. $upload=new Upload();//实例化上传类
  67. $upload->maxSize= 553145728 ;//设置上传大小
  68. $upload->exts=array('png','jpg','gif','jpeg');//设置文件上传类型
  69. $upload->savePath = './Uploads/'; // 设置附件上传(子)目录
  70. $upload->rootPath='./';//上传根目录
  71. $upload->saveName=array('uniqid','');//上传文件的保存规则,支持数组和字符串方式定义
  72. $info=$upload->uploadOne($_FILES['file']);
  73. if (!$info){
  74. $this->error($upload->getError());//上传错误提示信息
  75. }else{
  76. $data['src']=$info['savepath'].$info['savename'];
  77. $data['name']=$_FILES['file']['name'];
  78. $data['rname']=$info['savename'];
  79. }
  80. }
  81. if($first->save($data)){
  82. $this->success('修改文章成功',U('Article/lst'));
  83. }else{
  84. $this->error('修改文章失败');
  85. }
  86. return;
  87. }
  88. $firstid['id']=I('get.id');
  89. $firsts=$first->find($firstid);
  90. // var_dump($firsts);
  91. $this->assign('firsts',$firsts);
  92. $this->display();
  93. }
  94. }