Image upload

Home >PHP Framework >ThinkPHP >thinkphp5 image files are uploaded and displayed on the page

thinkphp5 image files are uploaded and displayed on the page

爆肝混世大魔王-流笙 第五人格服务
爆肝混世大魔王-流笙 第五人格服务Original
2021-03-12 17:50:49244browse

Image upload

Image:< br>


namespace app\ index\ controller;

use think\ Controller;

use app\ index\ model\ Image as ImageModel;

use think\ Request;

class Image extends controller {

public

function index() {

return view( 'index/sc' );

}

public

function image() {

// echo "image upload";

return view( 'Image/image' );

}

// protected $resultSetType = 'collection';

public

function upload() {

// Get form upload file e.g. upload 001.jpg

$file = request()->file( 'image' );

// Move to the framework application root directory /uploads/ directory

$info = $file->move( '../public' );

//Get the image path

$img = $info->getSaveName();

//Get the foreground data

$data = input( 'post.' );

//Splice the image path into the array

$c = $data [ 'image' ] = $img;

//Into the library

$b = new ImageModel();

$b->images = $c;

if ( $b->save() ) {

return 'Add successfully';

} else {

return 'Add failed';

}

}

public

function list() {

$b = new ImageModel();//Connect to the database

$c= "SELECT * FROM `image` \n"//Query data sql statement

. "ORDER BY `image`.`images` ASC";//think native query Method

$res = $b->query($c);

$this->assign( [//Render the found data to the page

'a' => $res

] );

return $this->fetch( 'index/cs' );//The cs page rendered to the index module

}

}

Display image

{volist name="a" id="a" offset="0" length="a"}

{/volist}

namespace app\index\model;

class Image extends \think\Model

{

// Set the complete data table name corresponding to the current model

protected $table = 'image';

// Set the database connection of the current model

protected $connection = [

// Database type

'type' => 'mysql',

// Server address

                                                                                                                                                                                              ’ ’ ’ ’ ’ s ’ ’ s ’               ’ ’ ’ ’ ’ ’ ’ s ’ ’ ’ s ‐ ‐ ‐ to 127.0.0.1',

##                    Username

          'username'   => 'image',

                                                                            use   with use using                   out out through through through off ‐ ‐ ‐‐ ‐‐‐ ` to // Database coding The default uses UTF8

# 'Charset' = & GT; 'UTF8',

## // database table prefix

'prefix' = & gt; 'Images'

// Database debugging mode

'debug' => true,

];

}

The above is the detailed content of thinkphp5 image files are uploaded and displayed on the page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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