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

thinkphp5 image files are uploaded and displayed on the page

Mar 12, 2021 pm 05:50 PM
1

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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24