How to implement image upload operation in ThinkPHP

不言
Release: 2023-03-30 09:38:01
Original
1329 people have browsed it

This article mainly introduces the method of ThinkPHP to implement image upload operation, and analyzes in detail the specific steps and related operation techniques of thinkPHP image upload operation. Friends in need can refer to the following

The examples of this article describe the implementation of ThinkPHP How to upload images. Share it with everyone for your reference. The details are as follows:

Let’s go directly to the example, which includes uploading a single image file, uploading multiple image files, and deleting some files. When deleting the database, just delete it. The file path in the database, instead of deleting the files in the server together, causing the server to explode,

Custom method in function.php in the common folder in TP:

maxSize = $maxSize;// 设置附件上传大小 $upload->exts = $exts; //array('jpg', 'gif', 'png', 'jpeg'); 设置附件上传类型 $upload->savePath = $savePath; // 设置附件上传目录 // 上传文件 //如果单个文件还是多个文件 if($file){ $info = $upload->uploadOne($file); }else{ $info = $upload->upload(); } //判定是否文件上传成功de if(!$info) { return false; }else{ // 上传成功, return $info; } } //上传图片 function fab_upload($files ,$maxSize = 0,$exts = null,$savePath = '') { //判定文件信息是否为空 if(empty($files)){ return false; } if($exts === null){ $exts = array('jpg', 'gif', 'png', 'jpeg'); }else{ $exts = 0; } $tmp = array(); //将文件信息(数组)用foreach循环遍历, foreach($files as $k => $v){ //判定文件大于0之后,将遍历value作为参数传入upload方法 if($v['size'] > 0){ $res = upload($v,$maxSize,$exts,$savePath); //如果传入成功就会将文件存储路径传入数组$tmp[]之中 if($res){ $tmp[$k] = $res['savepath'].$res['savename']; } } } //将存储传入文件路径的数组return回去 return $tmp; } ?>
Copy after login

In fact, no matter which file is uploaded, it needs to be controlled by the $_FILES variable area.

The above method is fab_upload calling the upload method;

In HTML, our form is written by Jiang Zi:

Copy after login

How to process uploaded files in the controller (splicing path and file name, and files that need to be deleted if the storage fails, similar to a callback)

/*调用写好的方法进行验证*/ $new_thumb = fab_upload($_FILES); // var_dump($new_thumb);die; $input['data']['addtime']=time();//生成申请时间 $input['data']['pretime']=strtotime($input['data']['pretime']);//将传过来的日期转换成时间戳 if($new_thumb && count($new_thumb) > 0){ $input['data'] = array_merge($input['data'],$new_thumb); } $f = $customer->add($input['data']); if($f){ $this->display('Index/infosuccess'); // $this->success("添加成功!",U('Index/infocheck',array('iccid'=>$input['data']['iccid']))); }else{//数据添加失败即删除照片 if($new_thumb){ $p = C('UNLINK_PATH').$new_thumb; unlink($p); } $this->error("添加失败!证件可能已存在"); }
Copy after login

The UNLINK_PATH variable is in ThinkPHP It is defined in the config file and comes from the path

 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 'DB_NAME' => 'urban', // 数据库名 'DB_USER' => 'root', // 用户名 'DB_PWD' => '123456', // 密码 'DB_PORT' => 3306, // 端口 'DB_PREFIX' => 'fab_', // 数据库表前缀 'DB_CHARSET'=> 'utf8', // 字符集 'CHECK_ROOT' => true, //开启rbac权限 'TMPL_CACHE_ON' => false, // 是否开启模板编译缓存,设为false则每次都会重新编译 'ACTION_CACHE_ON' => false, // 默认关闭Action 缓存 'HTML_CACHE_ON' => false, // 默认关闭静态缓存 'FILE_PATH'=>'http://localhost/urban/Uploads/', 'WEB_PATH' => 'http://localhost/urban/index.php/', 'WEB_URL' => 'http://localhost/urban/', 'UNLINK_PATH' => './Uploads/', 'PWD_KEY' => 'jeiskAsdlLsdfqaiocvwphxzbtu', 'AUTO_LOGIN_TIME'=>3600 * 24 * 7, 'SHOW_PAGE_TRACE'=>true, //追踪模式 'MY_CATCH_DIR' =>'./cache/', //缓存目录 'CODE_PATH' =>'http://localhost/urban/fabp/phpqrcode/', // 存放二维码的目录 'qq_face' =>'http://localhost/urban/Public/site/images/arclist/', //qq表情路径 'wxlogin' => array( 'appid' => 'wx35f5b9e9b90539ae', 'AppSecret' => '4de424bee1529a8abeda9c0c52aad3aa', 'callback' => 'http://localhost/urban/index.php/Home/Login/call_back.html' ), 'topic_pass'=>false, //是否开启话题审核 );
Copy after login

After adding it, it is natural to add and delete the function on the background management module

When displaying the image above, use the absolute path of the HTTP protocol The pictures are spliced together to display;

The deletion of pictures is based on the entry file index.php, which is the upload folder under the current folder;

Remember to call upload in ThinkPHP , the uploadone method returns only the storage location of the uploaded file under the upload folder, "'2016-09-02/57c94e71f0916.png'" (I think this is also the storage location)

So whether it is deleted or displayed, Need to use C method to splice it

if(IS_POST){ $input=I('post.'); $ids=implode(',',$input['id']); $brand=D('brand'); $img=$brand->where("brand_id in ($ids)")->getField('thumb',true); foreach($img as $v){ $p = C('UNLINK_PATH').$v; unlink($p); } $res=$brand->where("brand_id in ($ids)")->delete(); if($res){ $this->success("删除运营商品牌成功!"); }else{ $this->error("删除运营商品牌失败!"); } }
Copy after login

The reason why foreach is used; is because the ID passed is not the only one; it is multiple selection, delete;

Multiple selection, and passed to the corresponding column How is the value of the ID implemented?

     {$v.brand_name}       
Copy after login

The javascript method deleted above is written like this:

Copy after login

Additional: In fact, it is best to use this data to determine whether the file has been uploaded:

$_FILES['input_name']['size']
Copy after login

Is it greater than zero;

I can see a bigger world.

Related recommendations:

thinkPHP implemented Example of three-level linkage function in provinces and municipalities

ThinkPHP implements one-click cache clearing method

ThinkPHP file upload example

The above is the detailed content of How to implement image upload operation in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!