Pictures uploaded to the database
The common.php under the app
Apply Public files
* $name The name value for the form upload
* $filePath the folder name below uploads/for saving in the Portal folder public, no words will be created automatically
* $width Specify the thumbnail width
* $height Specify the thumbnail height
* Automatically generated thumbnails saved in the Thumb folder under the $filepath folder, automatically created
* @return Array One is the picture path, one is the thumbnail path, as follows:
* Array (2) {
["img"] = + string ("Uploads/img/20171211\3d4ca4098a8fb0f90e5f53fd7cd71535.jpg")
["thumb_img"] = + string ("Uploads/img/thumb/20171211/3d4ca4098a8fb0f90e5f53fd7cd71535.jpg")
}
*/
functionUploadFile ($name,$filePath,$width,$height)
{
$file= request () -file ($name);
if ($file) {
$filePaths= Root_path. ' Public '. DS. ' uploads '. DS.$filePath;
if (!file_exists ($filePaths)) {
mkdir ($filePaths,0777,true);
}
$info= $file -Move ($filePaths);
if ($info) {
$imgpath= ' uploads/'.$filePath.'/'.$info -getsavename ();
$image= \think\Image::Open ($imgpath);
$date _path= ' uploads/'.$filePath.'/thumb/'.Date (' YMD ');
if (!file_exists ($date _path)) {
mkdir ($date _path,0777,true);
}
$thumb _path= $date _path.'/'.$info -GetFileName ();
$image -Thumb ($width,$height) -Save ($thumb _path);
$data [' img ']= $imgpath;
$data [' thumb_img ']= $thumb _path;
return$data;
}else{
//upload failed to get error message
return$file -GetError ();
}
}
}
inside the controller.
Add to
Public function Add () {
return $this->fetch ("add");
}
Public Function Doadd () {
$path = uploadfile (' file ', ' images ', 80,80);
$data = input (' post. ');
$data = Array_merge ($data, $path);
$result = Db (' menu ')->insert ($data);
if ($result) {
$this->success ("Add Success", '/index/reply/index ');
}else{
$this->error ("Add Failure", '/index/reply/index ');
}
}
Save a picture to a database to generate a thumbnail image