Coder Social home page Coder Social logo

f-bikesystem's Introduction

F-BaiQiZhe

🚲

前台展示

后台展示

有关图片本地上传修改并删除原文件代码

ModelArticle.php

class Article extends Model
{
    protected static function init(){
	Article::event('before_insert',function($data){
	    if($_FILES['pic']['tmp_name']){
		$file=request()->file('pic');
		$info=$file->move(ROOT_PATH.'public'.DS.'uploads');
		if($info){
		    $pic='/uploads'.'/'.date('Ymd').'/'.$info->getFilename();
		    $data['pic']=$pic;
		}
	    }
	});

    Article::event('before_update',function($data){
        if($_FILES['pic']['tmp_name']){
            //删除原有的图片
            $arts=Article::find($data->id);
            $picpath=$_SERVER['DOCUMENT_ROOT'].$arts['pic'];
            //判断文件是否存在file_exists
            if(file_exists($picpath)){
                @unlink($picpath);
            }
            $file=request()->file('pic');
            $info=$file->move(ROOT_PATH.'public'.DS.'uploads');
            if($info){
                $pic='/uploads'.'/'.date('Ymd').'/'.$info->getFilename();
                $data['pic']=$pic;
            }
        }
    });

    Article::event('before_delete',function($data){
            //删除原有的图片
            $arts=Article::find($data->id);
            $picpath=$_SERVER['DOCUMENT_ROOT'].$arts['pic'];
            //判断文件是否存在file_exists
            if(file_exists($picpath)){
                @unlink($picpath);
            }
    });
}

}

ControllerArticle.php

public  function edit()
    {
	if(request()->isPost()){
	    $article=new ArticleModel();
	    $save=$article->update(input('post.'));
	    $va=\think\Loader::validate('Article');
	    if(!$va->check(input('post.'))){
		return $this->error($va->getError(input('post.')));
	    }
	    if($save){
		$this->success('修改文章成功!','lis','',1);
	    }else{
		$this->error('修改文章失败!');
	    }
	    return;
	}
	$cate=new CateModel();
	$cateres=$cate->catetree();
	$id=input('id');
	$artres=db('article')->find($id);
	$this->assign(array(
	    'artres' => $artres,
	    'cateres' => $cateres
	));

   return $this->fetch();
  }

展示图片只是便于本人好认识它...

f-bikesystem's People

Contributors

11003 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.