thinkphp怎么实现上传多个文件到不同的目录

2015-02-12 15:58:00

index.html

  1. <form method="POST" enctype="multipart/form-data" action="{:U('Index/upload')}">
  2.     <tr >
  3.         <td>flv文件</td>
  4.         <td><input type="file" name="flv" /></td>
  5.     </tr>
  6.     <tr >
  7.         <td>视频压缩文件</td>
  8.         <td><input type="file" name="movie" /></td>
  9.     </tr>
  10.     <tr>
  11.         <td>缩略图</td>
  12.         <td><input type="file" name="img" /></td>
  13.     </tr>
  14.     <input type="submit" value="上传" />
  15. </form>

复制代码

复制代码

UploadFile.class.php 162行

  1.            if (!is_dir($savePath)) {
  2.                 // 检查目录是否编码后的
  3.                 if (is_dir(base64_decode($savePath))) {
  4.                     $savePath = base64_decode($savePath);
  5.                 } else {
  6.                     // 尝试创建目录
  7.                     if (!mkdir($savePath)) {
  8.                         $this->error = '上传目录' . $savePath . '不存在';
  9.                         return false;
  10.                     }
  11.                 }
  12.             } else {
  13.                 if (!is_writeable($savePath)) {
  14.                     $this->error = '上传目录' . $savePath . '不可写';
  15.                     return false;
  16.                 }
  17.             }

复制代码

复制代码

改成

  1.         if(!is_array($savePath)){
  2.            if (!is_dir($savePath)) {
  3.                 // 检查目录是否编码后的
  4.                 if (is_dir(base64_decode($savePath))) {
  5.                     $savePath = base64_decode($savePath);
  6.                 } else {
  7.                     // 尝试创建目录
  8.                     if (!mkdir($savePath)) {
  9.                         $this->error = '上传目录' . $savePath . '不存在';
  10.                         return false;
  11.                     }
  12.                 }
  13.             } else {
  14.                 if (!is_writeable($savePath)) {
  15.                     $this->error = '上传目录' . $savePath . '不可写';
  16.                     return false;
  17.                 }
  18.             }
  19.         }

复制代码

复制代码

UploadFile.class.php 194行

  1. $file['savepath'] = $savePath;

复制代码

复制代码

改成

  1. $file['savepath'] = is_array($savePath)?$savePath[$key]:$savePath;

复制代码

复制代码

  1.     Public function upload() {
  2.         import('Org.Net.UploadFile');
  3.         $upload = new \Org\Net\UploadFile(); // 实例化上传类
  4.         $upload->maxSize = 3145728; // 设置附件上传大小
  5.         $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
  6. //        $upload->savePath = './Public/Uploads/'; // 设置附件上传目录
  7.         $upload->savePath = array('flv'=>'./Public/Uploads/flv/','movie'=>'./Public/Uploads/movie/','img'=>'./Public/Uploads/img/');
  8.         if (!$upload->upload()) {
  9.             $this->error($upload->getErrorMsg());
  10.         } else {// 上传成功 获取上传文件信息
  11.             $info = $upload->getUploadFileInfo();
  12.         }
  13.  
  14.     }

复制代码

复制代码

$upload->savePath   改为数组
./Public/Uploads/flv/
./Public/Uploads/movie/
./Public/Uploads/img/
这3个目录要手动创建 这样在就变成不影响原来程序的
情况下实现上传多个文件到不同的目录了

点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注