这是原来的两张表:表1 表2
这是新的表 表3:希望把图片上传样式如表3
下面是原来图片上传写法:(分表插入)
PictureMode.class.php<?php
namespace Admin\Model;
use Think\Model;
use Think\Upload;
// 图片模型
//负责图片的上传
class PictureModel extends Model{
protected $tableName = 'weibo1';
protected $_auto = array(
array('status', 1, self::MODEL_INSERT),
array('create_time', NOW_TIME, self::MODEL_INSERT),
);
/**
* 文件上传
* @param array $files 要上传的文件列表(通常是$_FILES数组)
* @param array $setting 文件上传配置
* @param string $driver 上传驱动名称
* @param array $config 上传驱动配置
* @return array 文件上传成功后的信息
*/
public function upload($files, $setting, $driver = 'Local', $config = null){
/* 上传文件 */
$setting['callback'] = array($this, 'isFile');
$setting['removeTrash'] = array($this, 'removeTrash');
$Upload = new Upload($setting, $driver, $config); foreach ($files as $key => $file) {
$ext = strtolower($file['ext']);
if(in_array($ext, array('jpg','jpeg','bmp','png'))){
hook('dealPicture',$file['tmp_name']);
}
}
$info = $Upload->upload($files); if($info){ //文件上传成功,记录文件信息
foreach ($info as $key => &$value) {
/* 已经存在文件记录 */
if(isset($value['id']) && is_numeric($value['id'])){
continue;
}
/* 记录文件信息 */
if(strtolower($driver)=='sae'){
$value['path'] = $config['rootPath'].'Picture/'.$value['savepath'].$value['savename']; //在模板里的url路径
}else{
if(strtolower($driver) != 'local'){
$value['path'] =$value['url'];
}
else{
$value['path'] = (substr($setting['rootPath'], 1).$value['savepath'].$value['savename']); //在模板里的url路径
}
}
$value['type'] = strtolower($driver); if($this->create($value) && ($path = $this->add())){
$value['path'] = $path;
} else {
//TODO: 文件上传成功,但是记录文件信息失败,需记录日志
unset($info[$path]);
}
}
foreach($info as &$t_info){
if($t_info['type'] =='local'){
$t_info['path']=fixAttachUrl($t_info['path']);
}
else{
$t_info['path']=$t_info['path'];
}
}
return $info; //文件上传成功
} else {
$this->error = $Upload->getError();
return false;
}
}
/**
* 检测当前上传的文件是否已经存在
* @param array $file 文件上传数组
* @return boolean 文件信息, false - 不存在该文件
*/
public function isFile($file){
if(empty($file['md5'])){
throw new \Exception('缺少参数:md5');
}
/* 查找文件 */
$map = array('md5' => $file['md5'],'sha1'=>$file['sha1'],);
return $this->field(true)->where($map)->find();
}
/**
* 清除数据库存在但本地不存在的数据
* @param $data
*/
public function removeTrash($data){
$this->where(array('id'=>$data['id'],))->delete();
}
}WeiboModel.calss.php public function addWeibo($uid, $content = '', $type = 'feed', $feed_data = array(), $from = '')
{
//写入数据库
$data = array('uid' => $uid, 'content' => $content, 'type' => $type, 'data' => serialize($feed_data), 'from' => $from);
$data = $this->create($data);
if (!$data) return false;
$weibo_id = $this->add($data);
//返回微博编号
return $weibo_id;
}希望指教一下如何批量插入如表3,想了自己的几个办法都没有成功。。
这是新的表 表3:希望把图片上传样式如表3
下面是原来图片上传写法:(分表插入)
PictureMode.class.php<?php
namespace Admin\Model;
use Think\Model;
use Think\Upload;
// 图片模型
//负责图片的上传
class PictureModel extends Model{
protected $tableName = 'weibo1';
protected $_auto = array(
array('status', 1, self::MODEL_INSERT),
array('create_time', NOW_TIME, self::MODEL_INSERT),
);
/**
* 文件上传
* @param array $files 要上传的文件列表(通常是$_FILES数组)
* @param array $setting 文件上传配置
* @param string $driver 上传驱动名称
* @param array $config 上传驱动配置
* @return array 文件上传成功后的信息
*/
public function upload($files, $setting, $driver = 'Local', $config = null){
/* 上传文件 */
$setting['callback'] = array($this, 'isFile');
$setting['removeTrash'] = array($this, 'removeTrash');
$Upload = new Upload($setting, $driver, $config); foreach ($files as $key => $file) {
$ext = strtolower($file['ext']);
if(in_array($ext, array('jpg','jpeg','bmp','png'))){
hook('dealPicture',$file['tmp_name']);
}
}
$info = $Upload->upload($files); if($info){ //文件上传成功,记录文件信息
foreach ($info as $key => &$value) {
/* 已经存在文件记录 */
if(isset($value['id']) && is_numeric($value['id'])){
continue;
}
/* 记录文件信息 */
if(strtolower($driver)=='sae'){
$value['path'] = $config['rootPath'].'Picture/'.$value['savepath'].$value['savename']; //在模板里的url路径
}else{
if(strtolower($driver) != 'local'){
$value['path'] =$value['url'];
}
else{
$value['path'] = (substr($setting['rootPath'], 1).$value['savepath'].$value['savename']); //在模板里的url路径
}
}
$value['type'] = strtolower($driver); if($this->create($value) && ($path = $this->add())){
$value['path'] = $path;
} else {
//TODO: 文件上传成功,但是记录文件信息失败,需记录日志
unset($info[$path]);
}
}
foreach($info as &$t_info){
if($t_info['type'] =='local'){
$t_info['path']=fixAttachUrl($t_info['path']);
}
else{
$t_info['path']=$t_info['path'];
}
}
return $info; //文件上传成功
} else {
$this->error = $Upload->getError();
return false;
}
}
/**
* 检测当前上传的文件是否已经存在
* @param array $file 文件上传数组
* @return boolean 文件信息, false - 不存在该文件
*/
public function isFile($file){
if(empty($file['md5'])){
throw new \Exception('缺少参数:md5');
}
/* 查找文件 */
$map = array('md5' => $file['md5'],'sha1'=>$file['sha1'],);
return $this->field(true)->where($map)->find();
}
/**
* 清除数据库存在但本地不存在的数据
* @param $data
*/
public function removeTrash($data){
$this->where(array('id'=>$data['id'],))->delete();
}
}WeiboModel.calss.php public function addWeibo($uid, $content = '', $type = 'feed', $feed_data = array(), $from = '')
{
//写入数据库
$data = array('uid' => $uid, 'content' => $content, 'type' => $type, 'data' => serialize($feed_data), 'from' => $from);
$data = $this->create($data);
if (!$data) return false;
$weibo_id = $this->add($data);
//返回微博编号
return $weibo_id;
}希望指教一下如何批量插入如表3,想了自己的几个办法都没有成功。。
1.上传图片的时候, 打印出返回的值
2.插入数据到表的时候,打印sql
一步步调试
http://www.sucaihuo.com/php/127-0-0-0