示例#1
0
 /**
  * The most basic file transaction: add a single entry (file or directory) to
  * the archive.
  *
  * @param bool $isVirtual If true, the next parameter contains file data instead of a file name
  * @param string $sourceNameOrData Absolute file name to read data from or the file data itself is $isVirtual is true
  * @param string $targetName The (relative) file name under which to store the file in the archive
  * @return True on success, false otherwise
  */
 protected function _addFile($isVirtual, &$sourceNameOrData, $targetName)
 {
     // Are we connected to a server?
     if (!is_resource($this->_sftphandle)) {
         if (!$this->_connectSFTP()) {
             return false;
         }
     }
     // See if it's a directory
     $isDir = $isVirtual ? false : is_dir($sourceNameOrData);
     if ($isDir) {
         // Just try to create the remote directory
         return $this->_makeDirectory($targetName);
     } else {
         // We have a file we need to upload
         if ($isVirtual) {
             // Create a temporary file, upload, rename it
             $tempFileName = AEUtilTempfiles::createRegisterTempFile();
             if (function_exists('file_put_contents')) {
                 // Easy writing using file_put_contents
                 if (@file_put_contents($tempFileName, $sourceNameOrData) === false) {
                     $this->setError('Could not store virtual file ' . $targetName . ' to ' . $tempFileName . ' using file_put_contents() before uploading.');
                     return false;
                 }
             } else {
                 // The long way, using fopen() and fwrite()
                 $fp = @fopen($tempFileName, 'wb');
                 if ($fp === false) {
                     $this->setError('Could not store virtual file ' . $targetName . ' to ' . $tempFileName . ' using fopen() before uploading.');
                     return false;
                 } else {
                     $result = @fwrite($fp, $sourceNameOrData);
                     if ($result === false) {
                         $this->setError('Could not store virtual file ' . $targetName . ' to ' . $tempFileName . ' using fwrite() before uploading.');
                         return false;
                     }
                     @fclose($fp);
                 }
             }
             // Upload the temporary file under the final name
             $res = $this->_upload($tempFileName, $targetName);
             // Remove the temporary file
             AEUtilTempfiles::unregisterAndDeleteTempFile($tempFileName, true);
             return $res;
         } else {
             // Upload a file
             return $this->_upload($sourceNameOrData, $targetName);
         }
     }
 }