解决方案 »

  1.   

    可以贴出你不明白的地方,thinkphp官网有上传文件使用示例。
      

  2.   


    控制器中处理新增相册模块
    function album_add() {
            if (!session('?user')) {
                $this->error('无权限操作!', "", 3);
            }
            if (IS_POST) {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $d_root = $_SERVER['DOCUMENT_ROOT'];
                $upload->rootPath  =     $d_root . '/upload/album/'; // 设置附件上传根目录
                $upload->savePath  =     ''; // 设置附件上传(子)目录
                // 上传文件
                $info   =   $upload->upload();
                $images = "";
                $cover_image = "";
                $index_cover = "";            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                } else {
                    if (is_array($info)) {
                        foreach($info as $key=>$value) {
                            if (strpos($key,"album_cover_add") !== FALSE) {
                                $cover_image = '/upload/album/' . $value['savepath'] . $value['savename'];
                                $thumb_image_path = '/upload/album/' . $value['savepath'] .'admin_cover/';
                                $image_name = $value['savename'];
                                create_dir("$d_root.$thumb_image_path");
                                $image = new \Think\Image();
                                $image->open("$d_root$cover_image");
                                // 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg
                                $image->thumb(150, 150,\Think\Image::IMAGE_THUMB_CENTER)->save("$d_root$thumb_image_path$image_name");
                                $admin_cover_path = $thumb_image_path . $image_name;
                            } else {
                                $images .= '/upload/album/' . $value['savepath'] . $value['savename'] . "|";
                                $images_path = '/upload/album/' . $value['savepath'] . $value['savename'];
                                $index_cover_path = '/upload/album/' . $value['savepath'] . 'index_cover/';
                                $image_name = $value['savename'];
                                create_dir("$d_root.$index_cover_path");
                                $image = new \Think\Image();
                                $image->open("$d_root$images_path");
                                // 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg
                                $image->thumb(150, 150,\Think\Image::IMAGE_THUMB_CENTER)->save("$d_root$index_cover_path$image_name");
                                $index_cover .= $index_cover_path . $image_name . "|";
                            }
                        }
                    }
                }
                $nowTime = date('y-m-d h:i:s',time());
                $a['title'] = I('post.album_title_add');
                $a['cover_image'] = $admin_cover_path;
                $a['index_cover_image'] = $index_cover;
                $a['images'] = $images;
                $a['add_time'] = $nowTime;
                $a['last_update_time'] = $nowTime;
                $album = M("album");
                $album->create($a);
                $result = $album->add();
                if($result){
                    //设置成功后跳转页面的地址,默认的返回页面是$_SERVER['HTTP_REFERER']
                    $this->success('相册发布成功!', "album_list", 3);
                } else {
                    //错误页面的默认跳转页面是返回前一页,通常不需要设置
                    $this->error('相册发布失败!', "", 5);
                }
            } else {
                $this->display("album_add");
            }
        }
    页面模板:
    <form action="album_add" method="post" enctype="multipart/form-data">    <table width="80%" height="100%" id="album_table_add">
            <tr>
                <td>相册标题:</td>
                <td><input type="text" name="album_title_add" id="album_title_add" size="50"/></td>
            </tr>
            <tr>
                <td>相册封面:</td>
                <td>
                    <input type="file" name="album_cover_add" id="album_cover_add" value=""/>
                </td>
            </tr>
            <tr>
                <td>上传图片:</td>
                <td>
                    <input type="file" name="album_images_add_3" id="album_images_add_3" value=""/>
                    <input type="button" name="album_rows_add_3" id="album_rows_add_3" value="添加" onclick="add_row(3)"/>
                </td>
            </tr>
            <tr>
                <td>后台管理封面尺寸:</td>
                <td>
                    宽:<input type="text" name="cover_width" id="cover_width" value="150"/>
                    高:<input type="text" name="cover_height" id="cover_height" value="150"/>
                </td>
            </tr>
            <tr>
                <td>前台首页封面尺寸:</td>
                <td>
                    宽:<input type="text" name="index_cover_width" id="index_cover_width" value="150"/>
                    高:<input type="text" name="index_cover_height" id="index_cover_height" value="150"/>
                </td>
            </tr>
            <tr>
                <td>前台列表尺寸:</td>
                <td>
                    宽:<input type="text" name="list_cover_width" id="list_cover_width" value="150"/>
                    高:<input type="text" name="list_cover_height" id="list_cover_height" value="150"/>
                </td>
            </tr>
            <tr>
                <td>前台详细页尺寸:</td>
                <td>
                    宽:<input type="text" name="detail_cover_width" id="detail_cover_width" value="150"/>
                    高:<input type="text" name="detail_cover_height" id="detail_cover_height" value="150"/>
                </td>
            </tr>
            <tr>
                <td colspan=2>
                    <input type="submit" id="album_submit_add" name="album_submit_add" value="添加相册" />
                </td>
            </tr>
        </table></form>