Image upload
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!