圖片上傳到數(shù)據(jù)庫
html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="index.php?g=Admin&m=ImageUpload&a=imageUpload" method="post" enctype="multipart/form-data">
UP:<input type="file" name="pic[]" value=""><br><br>
<input type="submit" value="submit"></input>
</input>
</form>
</body>
</html>
php
<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
//include "fileupload.class.php";
class ImageUploadController extends AdminbaseController {
function _initialize() {
parent::_initialize();
}
function index() {
$this->display();
}
//上傳圖片
function imageUpload() {
// $img = D('imgup');
$imgU = D("imgup"); //鏈接數(shù)據(jù)庫
$up = new \Think\fileupload();
//設(shè)置屬性(上傳的位置, 大小伙狐, 類型爷速, 名是是否要隨機(jī)生成)
$up->set("path", "C:\Users\Qyj\Desktop\image");
$up->set("maxsize", 2000000);
$up->set("allowtype", array("gif", "png", "jpg", "jpeg"));
$up->set("israndname", true);
//使用對象中的upload方法哨啃, 就可以上傳文件衬浑, 方法需要傳一個(gè)上傳表單的名子 pic, 如果成功返回true, 失敗返回false
if ($up->upload("pic")) {
echo '<pre>';
//獲取上傳后文件名子
//var_dump($up->getFileName($arr));
$picname = $up->getFileName();//獲取數(shù)組
$arr['img_name'] = $picname[0];//獲取數(shù)組中的第一個(gè)
echo $picname[0];
$imgU->add($arr); //添加到數(shù)據(jù)庫中
echo '</pre>';
} else {
echo '<pre>';
//獲取上傳失敗以后的錯(cuò)誤提示
var_dump($up->getErrorMsg());
echo '</pre>';
}
}
}
顯示本地圖片
image:<br><img src="../src/image/------***" width="250" height="200"/><br>
從MySQL中獲取數(shù)據(jù)并在html展示
html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<foreach name="img" item="vo">
<img src="./src/image/
------------------------
{$vo.img_name}"><br/>
</foreach>
</body>
</html>
php
<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
//include "fileupload.class.php";
class ImageUploadController extends AdminbaseController {
function _initialize() {
parent::_initialize();
}
function index() {
$this->display();
}
//調(diào)取圖片信息
function imageUP() {
$ku = D('imgup'); //鏈接服務(wù)器
// $i=$ku->where('id='.$s)->find();
$img = $ku->select();
$this->assign('img_name', $img);
$arr['img_name'] = $img[0];
echo '<pre>';
//var_dump($img);
$this->assign('img', $img);
$this->display();
echo '</pre>';
}
}