国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
1. Import the file
2. HTML part
3. Function implementation
4. Background processing
Picture upload
Save content
Home PHP Framework ThinkPHP How to add layui to thinkphp5 to implement image upload function

How to add layui to thinkphp5 to implement image upload function

May 28, 2023 am 11:13 AM
thinkphp layui

Many forms on the website will be used to upload pictures, logos, photos, and users will also upload pictures. At this time, the website needs a function to upload pictures, and after uploading, I hope to preview it to see if it is uploaded correctly.

thinkphp5 adds layui to implement the image upload function (with image preview), asynchronously transmit the image and preview it, return the asynchronously uploaded value to the hidden field of the form and then submit it.

1. Import the file

First of all, it is necessary to introduce the jQuery file

<link rel="stylesheet" type="text/css" href="__STATIC__/plugins/layui/css/layui.css" rel="external nofollow" >
<script type="text/javascript" src="__STATIC__/plugins/layui/layui.js"></script>

2. HTML part

<form class="layui-form">
<div class="layui-input-inline">
	<button class="layui-btn layui-btn-sm" onclick="return false;" id="upload_img"><i class="layui-icon">&#xe67c;</i>上傳圖片</button>
	<img id="pre_img" <?php if($data[&#39;item&#39;][&#39;img&#39;]){echo &#39;src="&#39;.$data[&#39;item&#39;][&#39;img&#39;].&#39;"&#39;;}?>  />
	<input type="hidden" name="img" value="{$data.item.img}">
</div>

	<div class="layui-form-item">
		<div class="layui-input-block">
			<button class="layui-btn" onclick="save()">保存</button>
		</div>
	</div>
</form>

3. Function implementation

<script type="text/javascript">
	layui.use([&#39;form&#39;,&#39;layer&#39;,&#39;upload&#39;],function(){
		$ = layui.jquery;
		var form = layui.form;
		layer = layui.layer;

		var upload = layui.upload;
   
		 //執(zhí)行實例
		var uploadInst = upload.render({
			elem: &#39;#upload_img&#39; //綁定元素
			,url: &#39;/admins.php/admins/video/upload_img&#39; //上傳接口
			,accept:&#39;images&#39;
			,done: function(res){
			  //上傳完畢回調(diào)
			  $(&#39;#pre_img&#39;).attr(&#39;src&#39;,res.msg);
			  $(&#39;input[name="img"]&#39;).val(res.msg);
			}
			,error: function(){
			  //請求異?;卣{(diào)
			}
		});
	});

	// 保存
	function save(){
		var title = $.trim($(&#39;input[name="title"]&#39;).val());
		var url = $.trim($(&#39;input[name="url"]&#39;).val());
		if(title == &#39;&#39;){
			layer.msg(&#39;請輸入影片名稱&#39;,{&#39;icon&#39;:2,&#39;anim&#39;:6});
			return;
		}
		if(url == &#39;&#39;){
			layer.msg(&#39;請輸入影片地址&#39;,{&#39;icon&#39;:2,&#39;anim&#39;:6});
			return;
		}
		$.post(&#39;/admins.php/admins/video/save&#39;,$(&#39;form&#39;).serialize(),function(res){
			if(res.code>0){
				layer.msg(res.msg,{&#39;icon&#39;:2,&#39;anim&#39;:2});
			}else{
				layer.msg(res.msg,{&#39;icon&#39;:1});
				setTimeout(function(){parent.window.location.reload();},1000);
			}
		},&#39;json&#39;);
	}
</script>

4. Background processing

Picture upload

public function upload_img(){
	$file = request()->file(&#39;file&#39;);
	if($file==null){
		exit(json_encode(array(&#39;code&#39;=>1,&#39;msg&#39;=>&#39;沒有文件上傳&#39;)));
	}
	$info = $file->move(ROOT_PATH.&#39;public&#39;.DS.&#39;uploads&#39;);
	$ext = ($info->getExtension());
	if(!in_array($ext,array(&#39;jpg&#39;,&#39;jpeg&#39;,&#39;gif&#39;,&#39;png&#39;))){
		exit(json_encode(array(&#39;code&#39;=>1,&#39;msg&#39;=>&#39;文件格式不支持&#39;)));
	}
	$img = &#39;/uploads/&#39;.$info->getSaveName();
	exit(json_encode(array(&#39;code&#39;=>0,&#39;msg&#39;=>$img)));
}

Save content

public function save(){
	$id = (int)input(&#39;post.id&#39;);
	$data[&#39;title&#39;] = trim(input(&#39;post.title&#39;));
	$data[&#39;channel_id&#39;] = (int)input(&#39;post.channel_id&#39;);
	$data[&#39;charge_id&#39;] = (int)input(&#39;post.charge_id&#39;);
	$data[&#39;area_id&#39;] = (int)input(&#39;post.area_id&#39;);
	$data[&#39;img&#39;] = trim(input(&#39;post.img&#39;));
	$data[&#39;url&#39;] = trim(input(&#39;post.url&#39;));
	$data[&#39;keywords&#39;] = trim(input(&#39;post.keywords&#39;));
	$data[&#39;desc&#39;] = trim(input(&#39;post.desc&#39;));
	$data[&#39;status&#39;] = (int)input(&#39;post.status&#39;);

	if($data[&#39;title&#39;] == &#39;&#39;){
		exit(json_encode(array(&#39;code&#39;=>1,&#39;msg&#39;=>&#39;影片名稱不能為空&#39;)));
	}
	if($data[&#39;url&#39;] == &#39;&#39;){
		exit(json_encode(array(&#39;code&#39;=>1,&#39;msg&#39;=>&#39;影片地址不能為空&#39;)));
	}

	if($id){
		$this->db->table(&#39;video&#39;)->where(array(&#39;id&#39;=>$id))->update($data);
	}else{
		$data[&#39;add_time&#39;] = time();
		$this->db->table(&#39;video&#39;)->insert($data);
	}
	exit(json_encode(array(&#39;code&#39;=>0,&#39;msg&#39;=>&#39;保存成功&#39;)));
}

The above is the detailed content of How to add layui to thinkphp5 to implement image upload function. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

How to transfer data in layui How to transfer data in layui Apr 26, 2024 am 03:39 AM

The method of using layui to transmit data is as follows: Use Ajax: Create the request object, set the request parameters (URL, method, data), and process the response. Use built-in methods: Simplify data transfer using built-in methods such as $.post, $.get, $.postJSON, or $.getJSON.

How layui implements self-adaptation How layui implements self-adaptation Apr 26, 2024 am 03:00 AM

Adaptive layout can be achieved by using the responsive layout function of the layui framework. The steps include: referencing the layui framework. Define an adaptive layout container and set the layui-container class. Use responsive breakpoints (xs/sm/md/lg) to hide elements under specific breakpoints. Specify element width using the grid system (layui-col-). Create spacing via offset (layui-offset-). Use responsive utilities (layui-invisible/show/block/inline) to control the visibility of elements and how they appear.

See all articles