示例#1
0
文件: file.php 项目: adjaika/J3Base
 /**
  * Moves an uploaded file to a destination folder
  *
  * @param   string   $src              The name of the php (temporary) uploaded file
  * @param   string   $dest             The path (including filename) to move the uploaded file to
  * @param   boolean  $use_streams      True to use streams
  * @param   boolean  $allow_unsafe     Allow the upload of unsafe files
  * @param   boolean  $safeFileOptions  Options to JFilterInput::isSafeFile
  *
  * @return  boolean  True on success
  *
  * @since   11.1
  */
 public static function upload($src, $dest, $use_streams = false, $allow_unsafe = false, $safeFileOptions = array())
 {
     if (!$allow_unsafe) {
         $descriptor = array('tmp_name' => $src, 'name' => basename($dest), 'type' => '', 'error' => '', 'size' => '');
         $isSafe = JFilterInput::isSafeFile($descriptor, $safeFileOptions);
         if (!$isSafe) {
             JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR03', $dest), JLog::WARNING, 'jerror');
             return false;
         }
     }
     // Ensure that the path is valid and clean
     $pathObject = new JFilesystemWrapperPath();
     $dest = $pathObject->clean($dest);
     // Create the destination directory if it does not exist
     $baseDir = dirname($dest);
     if (!file_exists($baseDir)) {
         $folderObject = new JFilesystemWrapperFolder();
         $folderObject->create($baseDir);
     }
     if ($use_streams) {
         $stream = JFactory::getStream();
         if (!$stream->upload($src, $dest)) {
             JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_UPLOAD', $stream->getError()), JLog::WARNING, 'jerror');
             return false;
         }
         return true;
     } else {
         $FTPOptions = JClientHelper::getCredentials('ftp');
         $ret = false;
         if ($FTPOptions['enabled'] == 1) {
             // Connect the FTP client
             $ftp = JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
             // Translate path for the FTP account
             $dest = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
             // Copy the file to the destination directory
             if (is_uploaded_file($src) && $ftp->store($src, $dest)) {
                 unlink($src);
                 $ret = true;
             } else {
                 JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR04', $src, $dest), JLog::WARNING, 'jerror');
             }
         } else {
             if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) {
                 // Short circuit to prevent file permission errors
                 if ($pathObject->setPermissions($dest)) {
                     $ret = true;
                 } else {
                     JLog::add(JText::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR01'), JLog::WARNING, 'jerror');
                 }
             } else {
                 JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR04', $src, $dest), JLog::WARNING, 'jerror');
             }
         }
         return $ret;
     }
 }