コード例 #1
0
ファイル: File.php プロジェクト: rtsantos/mais
 private function _clearFiles()
 {
     $folder = ZendT_Lib::getTmpDir() . '/files/';
     $dh = opendir($folder);
     while ($handle = readdir($dh)) {
         if (is_dir($folder . $handle) && $handle != '.' && $handle != '..') {
             $files = glob($folder . $handle . "/*.*");
             if (count($files) == 0) {
                 $path = $folder . $handle;
                 @rmdir($path);
             } else {
                 foreach ($files as $file) {
                     $lastAccess = filemtime($file);
                     // 30 minutos
                     if (time() - $lastAccess > 30 * 60 * 1) {
                         @unlink($file);
                     }
                 }
             }
         }
     }
     closedir($dh);
 }
コード例 #2
0
ファイル: FileController.php プロジェクト: rtsantos/mais
 public function pluploadAction()
 {
     $this->_helper->layout->disableLayout();
     $this->_helper->viewRenderer->setNoRender(true);
     $json = new ZendT_Json_Result();
     try {
         $targetDir = ZendT_Lib::getTmpDir() . '/files/' . DIRECTORY_SEPARATOR . "plupload";
         // Create target dir
         if (!file_exists($targetDir)) {
             @($result = mkdir($targetDir));
             if (!$result) {
                 throw new ZendT_Exception_Error('Não foi possível criar o diretório "$targetDir".', 1001);
             }
         }
         if (!file_exists($targetDir)) {
             @($result = mkdir($targetDir));
             if (!$result) {
                 throw new ZendT_Exception_Error('Não foi possível criar o diretório "$targetDir".', 1001);
             }
         }
         //$targetDir = 'uploads';
         $cleanupTargetDir = true;
         // Remove old files
         $maxFileAge = 1 * 3600;
         // Temp file age in seconds
         // 5 minutes execution time
         @set_time_limit(5 * 60);
         // Uncomment this one to fake upload time
         // usleep(5000);
         // Get parameters
         $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
         $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
         $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : '';
         // Clean the fileName for security reasons
         $fileName = removeAccent(trim($fileName));
         $fileName = preg_replace('/[^\\w\\._]+/', '_', $fileName);
         $fileName = str_replace(' ', '_', $fileName);
         // Make sure the fileName is unique but only if chunking is disabled
         if ($chunks < 2 && file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName)) {
             $ext = strrpos($fileName, '.');
             $fileName_a = substr($fileName, 0, $ext);
             $fileName_b = substr($fileName, $ext);
             $count = 1;
             while (file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName_a . '_' . $count . $fileName_b)) {
                 $count++;
             }
             $fileName = $fileName_a . '_' . $count . $fileName_b;
         }
         $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
         // Remove old temp files
         if ($cleanupTargetDir && is_dir($targetDir) && ($dir = opendir($targetDir))) {
             while (($file = readdir($dir)) !== false) {
                 $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
                 // Remove temp file if it is older than the max age and is not the current file
                 if (preg_match('/\\.part$/', $file) && filemtime($tmpfilePath) < time() - $maxFileAge && $tmpfilePath != "{$filePath}.part") {
                     @unlink($tmpfilePath);
                 }
             }
             closedir($dir);
         } else {
             throw new ZendT_Exception_Error('Failed to open temp directory.', 100);
         }
         // Look for the content type header
         if (isset($_SERVER["HTTP_CONTENT_TYPE"])) {
             $contentType = $_SERVER["HTTP_CONTENT_TYPE"];
         }
         if (isset($_SERVER["CONTENT_TYPE"])) {
             $contentType = $_SERVER["CONTENT_TYPE"];
         }
         // Handle non multipart uploads older WebKit versions didn't support multipart in HTML5
         if (strpos($contentType, "multipart") !== false) {
             if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
                 // Open temp file
                 $out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
                 if ($out) {
                     // Read binary input stream and append it to temp file
                     $in = fopen($_FILES['file']['tmp_name'], "rb");
                     if ($in) {
                         while ($buff = fread($in, 4096)) {
                             fwrite($out, $buff);
                         }
                     } else {
                         throw new ZendT_Exception_Error('Failed to open input stream.', 101);
                     }
                     fclose($in);
                     fclose($out);
                     @unlink($_FILES['file']['tmp_name']);
                 } else {
                     throw new ZendT_Exception_Error('Failed to open output stream.', 102);
                 }
             } else {
                 throw new ZendT_Exception_Error('Failed to move uploaded file.', 103);
             }
         } else {
             // Open temp file
             $out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
             if ($out) {
                 // Read binary input stream and append it to temp file
                 $in = fopen("php://input", "rb");
                 if ($in) {
                     while ($buff = fread($in, 4096)) {
                         fwrite($out, $buff);
                     }
                 } else {
                     throw new ZendT_Exception_Error('Failed to open input stream.', 104);
                 }
                 fclose($in);
                 fclose($out);
             } else {
                 throw new ZendT_Exception_Error('Failed to open output stream.', 105);
             }
         }
         // Check if file has been uploaded
         if (!$chunks || $chunk == $chunks - 1) {
             // Strip the temp .part suffix off
             @($result = rename("{$filePath}.part", $filePath));
             if (!$result) {
                 throw new ZendT_Exception_Error('Não foi possível renomear o arquivo.', 1002);
             }
         }
         if ($this->getRequest()->getParam('platform')) {
             $_file = new ZendT_File($fileName, file_get_contents($filePath), $_FILES['file']['type']);
             if (file_exists($filePath)) {
                 unlink($filePath);
             }
             $infoFile = $_file->toArrayJson();
             $infoFile['size'] = $_FILES['file']['size'];
             $infoFile['type'] = $_FILES['file']['type'];
             $json->setResult($infoFile);
         } else {
             $json->setResult(base64_encode($filePath));
         }
     } catch (Exception $Ex) {
         $json->setException($Ex);
     }
     echo $json->toRpc();
 }
コード例 #3
0
ファイル: File.php プロジェクト: rtsantos/mais
 /**
  * Cria o arquivo no servidor e retorna o caminho completo do mesmo
  *
  * @param string $name
  * @param string $content
  * @return \ZendT_File
  * @throws ZendT_Exception_Error 
  */
 public function create($name = '', $content = '', $type = '', $id = '')
 {
     $this->_name = str_replace(':', '', $name);
     if ($this->_name == '') {
         $this->_name = md5(date('dmyhis') . rand(200000, 9999999999)) . '.tmp';
     }
     $this->_type = $type;
     $this->setId($id);
     //  $filename = $this->getPathBase() . '/' . $this->getFolderBase() . '/' . $this->_name;
     $this->getFolderBase();
     $filename = ZendT_Lib::getTmpDir() . '/files' . '/' . $this->getFolderBase() . '/' . $this->_name;
     if (file_exists($filename)) {
         @unlink($filename);
     }
     @($result = fopen($filename, 'a+'));
     if (!$result) {
         $error = error_get_last();
         throw new ZendT_Exception_Error('Erro ao criar o arquivo.' . substr($error['message'], 0, 1000));
     }
     if ($content) {
         @($write = fwrite($result, $content));
         if (!$write) {
             $error = error_get_last();
             throw new ZendT_Exception_Error('Erro ao escrever no arquivo.' . substr($error['message'], 0, 1000));
         }
     }
     return $this;
 }