Save a file and return its id.

@param string $source
Source path relative to Drupal installation root.

@return \Drupal\Core\Entity\EntityInterface
Saved file object.
165862
protected function saveFile($source) {
    $name = basename($source);
    $path = realpath(DRUPAL_ROOT . '/' . $source);
    $uri = file_unmanaged_copy($path, 'public://' . $name, FILE_EXISTS_REPLACE);
    $file = File::create(['uri' => $uri]);
    $file->save();
    return $file;
  }
862253
public function importSourceNode($source)
    {
        $DOMDocument = new self();
        $DOMDocument->loadHTML($source);

        $this->metaNodes->import($DOMDocument->metaNodes);
        $this->headScriptNodes->import($DOMDocument->headScriptNodes);
        $this->bodyScriptNodes->import($DOMDocument->bodyScriptNodes);
        $this->linkNodes->import($DOMDocument->linkNodes);
        $this->styleContent->import($DOMDocument->styleContent);
        $this->headScriptContent->import($DOMDocument->headScriptContent);
        $this->bodyScriptContent->import($DOMDocument->bodyScriptContent);

        $bodyElement = $DOMDocument->getElementsByTagName('body')->item(0);

        if ($bodyElement->firstChild instanceof Dom\Element) {
            return $bodyElement->firstChild;
        } elseif ($bodyElement->firstChild instanceof \DOMText) {
            foreach ($bodyElement->childNodes as $childNode) {
                if ($childNode instanceof Dom\Element) {
                    return $childNode->cloneNode(true);
                    break;
                }
            }
        }

        return $bodyElement;
    }
593743
public function save($identifier, $contents)
	{
		// get the formatted output
		$output = $this->export_format($contents);

		if ( ! $output)
		{
			return false;
		}

		if ( ! $path = \Finder::search('lang', $identifier))
		{
			if ($pos = strripos($identifier, '::'))
			{
				// get the namespace path
				if ($path = \Autoloader::namespace_path('\\'.ucfirst(substr($identifier, 0, $pos))))
				{
					// strip the namespace from the filename
					$identifier = substr($identifier, $pos+2);

					// strip the classes directory as we need the module root
					$path = substr($path,0, -8).'lang'.DS.$identifier;
				}
				else
				{
					// invalid namespace requested
					return false;
				}
			}
		}

		// absolute path requested?
		if ($identifier[0] === '/' or (isset($identifier[1]) and $identifier[1] === ':'))
		{
			$path = $identifier;
		}

		// make sure we have a fallback
		$path or $path = APPPATH.'lang'.DS.$identifier;

		$path = pathinfo($path);
		if ( ! is_dir($path['dirname']))
		{
			mkdir($path['dirname'], 0777, true);
		}

		return \File::update($path['dirname'], $path['basename'], $output);
	}
346618
public function save($site)
    {

        if (is_array($site)) {
            $siteData = $site;
            if ($site['id'] > 0) {
                $site = $this->find($site['id']);
            } else {
                $site = new Site();
            }
            $this->assignArrayToObject($site, $siteData);
        } else if (!($site instanceof Site)) {
            $this->addErrorMessage(self::ERROR_WRONG_OBJECT);
        }

        if (!$this->hasError()) {
            $check = $this->siteManager->save($site);
            if ($check) {
                $this->addSuccessMessage(self::SUCCESS_SAVED);
            }
        }

        return $site;
    }
873466
private function handleDownload(OutputInterface $output, $source)
	{
		$this->writeln($output, "Downloading $source", OutputInterface::VERBOSITY_VERBOSE);

		return $this->unpack(\JInstallerHelper::downloadPackage($source));
	}
837338
public function getSavedFile()
    {
        if($this->savedAttribute && $this->owner->{$this->savedAttribute}){
            return FileModel::findOne($this->owner->{$this->savedAttribute});
        }
    }
1309937
public function saveFile($file, $contentId) {
    // Prepare directory
    if (empty($contentId)) {
      // Should be in editor tmp folder
      $path = $this->getEditorPath();
    }
    else {
      // Should be in content folder
      $path = $this->path . '/content/' . $contentId;
    }
    $path .= '/' . $file->getType() . 's';
    self::dirReady($path);

    // Add filename to path
    $path .= '/' . $file->getName();

    copy($_FILES['file']['tmp_name'], $path);

    return $file;
  }
1275281
public function install($source)
    {
        $pattern                             = chr(1) . '^' . preg_quote(JPATH_ROOT) . '/' . chr(1);
        $this->extensions[basename($source)] = preg_replace($pattern, '', $source);

        $xmlDirectory = $source . '/entities';
        $strategy     = new RecursiveDirectoryStrategy($xmlDirectory);
        $this->builder->addLocatorStrategy($strategy);
        $entityNames = $this->importDefinition($xmlDirectory . '/*.xml');

        foreach ($entityNames as $entityName) {
            $this->dataDirectories[$entityName] = $source . '/data';
        }

        return $entityNames;
    }
700184
protected function _preserveFilename(EntityInterface $entity, array $options = []) {
		$filename = $entity['filename'];
		if (!empty($options['filePrefix'])) {
			$filename = $options['filePrefix'] . $entity['filename'];
		}
		if (!empty($options['fileSuffix'])) {
			$split = $this->splitFilename($filename, true);
			$filename = $split['filename'] . $options['fileSuffix'];
			if ($options['preserveExtension'] === true) {
				$filename .= $split['extension'];
			}
		}

		return $filename;
	}
147398
function save($args)
    {
        $group = $args['group'];
        $args['status'] = false;

        if (!$args['path']) {
            $rcmail   = rcube::get_instance();
            $temp_dir = $rcmail->config->get('temp_dir');
            $tmp_path = tempnam($temp_dir, 'rcmAttmnt');

            if ($fp = fopen($tmp_path, 'w')) {
                fwrite($fp, $args['data']);
                fclose($fp);
                $args['path'] = $tmp_path;
            }
            else {
                return $args;
            }
        }

        $args['id']     = $this->file_id();
        $args['status'] = true;

        // Note the file for later cleanup
        $_SESSION['plugins']['filesystem_attachments'][$group][$args['id']] = $args['path'];

        return $args;
    }
582149
protected function _file($id)
    {
        $instance = self::getInstance();
        $fileNmae = $instance->_idToFileName($id);
        return self::cacheDir() . $fileNmae;
    }
449242
private function getFileFromDisk($directory, $source)
    {
        $hash = 'DISK-'.\md5(\realpath($source));
        if (!\array_key_exists($hash, $this->files)) {
            // Check if source is a directory or a file.
            if (\is_dir($source)) {
                $this->fs->mirror($source, $directory.'/'.$hash, null, ['copy_on_windows' => true]);
            } else {
                $this->fs->copy($source, $directory.'/'.$hash);
            }

            $this->files[$hash] = $hash;
        }

        return $this->files[$hash];
    }
287574
public function filename(EntityInterface $entity, array $options = []) {
		$config = array_merge($this->getConfig(), $options);
		if ($config['preserveFilename'] === true) {
			return $this->_preserveFilename($entity, $config);
		}

		return $this->_buildFilename($entity, $config);
	}
215869
public function getSaverForEntity(
        EntityInterface $entity
    ): EntitySaverInterface {
        $fqn = $this->getEntityNamespace($entity);

        return $this->getSaverForEntityFqn($fqn);
    }
291398
public function get_file_reference($source) {
        // Internally we store serialized value but user input is json-encoded for security reasons.
        $ref = json_decode(base64_decode($source));
        $filename  = clean_param($ref->filename, PARAM_FILE);
        $url = clean_param($ref->url, PARAM_URL);
        return base64_encode(serialize((object)array('url' => $url, 'filename' => $filename)));
    }
185030
protected function castStream($source): StreamInterface
    {
        if ($source instanceof UploadedFileInterface || $source instanceof StreamableInterface) {
            $source = $source->getStream();
        }

        if ($source instanceof StreamInterface) {
            //This step is important to prevent user errors
            $source->rewind();

            return $source;
        }

        if (is_resource($source)) {
            return stream_for($source);
        }

        if (empty($source)) {
            //Guzzle?
            return stream_for('');
        }

        if ($this->isFilename($source)) {
            //Must never pass user string in here, use Stream
            return stream_for(fopen($source, 'rb'));
        }

        //We do not allow source names in a string form
        throw new BucketException(
            "Source must be a valid resource, stream or filename, invalid value given"
        );
    }
717988
public function saveItem($entity)
    {
        $this->em->persist($entity);
        $this->em->flush();
        return $entity;
    }