Image upload
HomePHP FrameworkThinkPHPthinkphp5 image files are uploaded and displayed on the page

Image upload

Image:


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

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools