예제 #1
0
파일: File.php 프로젝트: Andyyang1981/pi
 /**
  * Send the file to the client (Download)
  *
  * @param string|array $source File or file meta to download
  * @param array $options Options for the file(s) to send
  *
  * @return Download
  * @see Pi\File\Download
  */
 public function download($source, array $options = array())
 {
     $downloader = new Download();
     $downloader->send($source, $options);
     return $downloader;
 }
예제 #2
0
 /**
  * Download media
  */
 public function indexAction()
 {
     // Disable debugger message
     Pi::service('log')->mute();
     $id = $this->params('id');
     $ids = array_filter(explode(',', $id));
     if (empty($ids)) {
         if (substr(PHP_SAPI, 0, 3) == 'cgi') {
             header('Status: 404 Not Found');
         } else {
             header('HTTP/1.1 404 Not Found');
         }
         exit;
     }
     // Export files
     $model = $this->getModel('doc');
     $rowset = $model->select(array('id' => $ids));
     $files = array();
     foreach ($rowset as $row) {
         $path = $row['path'];
         if (!$path || !file_exists($path)) {
             continue;
         }
         $files[] = array('source' => $path, 'filename' => $row['filename'], 'content_type' => $row['mimetype'], 'content_length' => $row['size']);
     }
     $model->increment('count', array('id' => $ids));
     if (1 == count($files)) {
         $files = current($files);
         $source = $files['source'];
         $options = $files;
     } else {
         $source = $files;
         $options = array();
     }
     // This code cannot output file
     $downloader = new Download();
     $result = $downloader->send($source, $options);
     if (false === $result) {
         if (substr(PHP_SAPI, 0, 3) == 'cgi') {
             header('Status: 404 Not Found');
         } else {
             header('HTTP/1.1 404 Not Found');
         }
     }
     exit;
     /*
     $filePath = 'upload/tmp';
     Pi::service('file')->mkdir(Pi::path($filePath));
     $filename = sprintf('%s/media-%s.zip', $filePath, time());
     $filename = Pi::path($filename);
     $zip      = new ZipArchive();
     if ($zip->open($filename, ZIPARCHIVE::CREATE)!== TRUE) {
         exit ;
     }
     $compress = count($files) > 1 ? true : false;
     if ($compress) {
         foreach ($files as $file) {
             if (file_exists($file)) {  
                 $zip->addFile($file, basename($file));
             }
         }  
         $zip->close();
     } else {
         $filename = Pi::path(array_shift($files));
     }
     
     $options = array(
         'file'       => $filename,
         'fileName'   => basename($filename),
     );
     if ($compress) {
         $options['deleteFile'] = true;
     }
     $this->httpOutputFile($options);
     */
 }