creates a subfolder in assets/ to store this sites files
@param Boolean $write - writes the site object if set to true
@return Int $folder->ID
162735
public function createAssetsSubfolder($write = false){
		$siteFolderName = singleton(URLSegmentFilter::class)->filter($this->Title);
		$folder = Folder::find_or_make($siteFolderName);

		if($write){
			$this->FolderID = $folder->ID;
			$this->write();
			if($this->isPublished()) $this->doPublish();
		}

		return $folder->ID;
	}
145382
function store( $atomic = false )
    {
        if ( $this->open( $atomic ) )
        {
            $this->write( "<?php\n" );

            $this->writeElements();

            $this->write( "?>\n" );

            $this->writeChunks();
            $this->flushChunks();
            $this->close();

            if ( !$this->ClusteringEnabled )
            {
                $perm = octdec( eZINI::instance()->variable( 'FileSettings', 'StorageFilePermissions' ) );
                chmod( eZDir::path( array( $this->PHPDir, $this->PHPFile ) ), $perm );
            }

            // Write log message to storage.log
            eZLog::writeStorageLog( $this->PHPFile, $this->PHPDir . '/' );
            return true;
        }
        else
        {
            eZDebug::writeError( "Failed to open file '" . $this->PHPDir . '/' . $this->PHPFile . "'", __METHOD__ );
            return false;
        }
    }
118914
public function save($src = null, $base = null, $overwrite = true)
    {
        if (isset($src)) {
            $this->setTarget($src, $base);
        }

        if ($overwrite === false && is_file($this->cacheFileName)) {
            $this->Log("Not overwriting file since its already exists and \$overwrite if false.");
            return;
        }

        is_writable($this->saveFolder)
            or $this->raiseError('Target directory is not writable.');

        $type = $this->getTargetImageExtension();
        $this->Log("Saving image as " . $type);
        switch($type) {

            case 'jpeg':
            case 'jpg':
                $this->Log("Saving image as JPEG to cache using quality = {$this->quality}.");
                imagejpeg($this->image, $this->cacheFileName, $this->quality);

                // Use JPEG optimize if defined
                if ($this->jpegOptimizeCmd) {
                    if ($this->verbose) {
                        clearstatcache();
                        $this->log("Filesize before optimize: " . filesize($this->cacheFileName) . " bytes.");
                    }
                    $res = array();
                    $cmd = $this->jpegOptimizeCmd . " -outfile $this->cacheFileName $this->cacheFileName";
                    exec($cmd, $res);
                    $this->log($cmd);
                    $this->log($res);
                }
                break;

            case 'gif':
                $this->Log("Saving image as GIF to cache.");
                imagegif($this->image, $this->cacheFileName);
                break;

            case 'webp':
                $this->Log("Saving image as WEBP to cache using quality = {$this->quality}.");
                imagewebp($this->image, $this->cacheFileName, $this->quality);
                break;

            case 'png':
            default:
                $this->Log("Saving image as PNG to cache using compression = {$this->compress}.");

                // Turn off alpha blending and set alpha flag
                imagealphablending($this->image, false);
                imagesavealpha($this->image, true);
                imagepng($this->image, $this->cacheFileName, $this->compress);

                // Use external program to process lossy PNG, if defined
                $lossyEnabled = $this->pngLossy === true;
                $lossySoftEnabled = $this->pngLossy === null;
                $lossyActiveEnabled = $this->lossy === true;
                if ($lossyEnabled || ($lossySoftEnabled && $lossyActiveEnabled)) {
                    if ($this->verbose) {
                        clearstatcache();
                        $this->log("Lossy enabled: $lossyEnabled");
                        $this->log("Lossy soft enabled: $lossySoftEnabled");
                        $this->Log("Filesize before lossy optimize: " . filesize($this->cacheFileName) . " bytes.");
                    }
                    $res = array();
                    $cmd = $this->pngLossyCmd . " $this->cacheFileName $this->cacheFileName";
                    exec($cmd, $res);
                    $this->Log($cmd);
                    $this->Log($res);
                }

                // Use external program to filter PNG, if defined
                if ($this->pngFilterCmd) {
                    if ($this->verbose) {
                        clearstatcache();
                        $this->Log("Filesize before filter optimize: " . filesize($this->cacheFileName) . " bytes.");
                    }
                    $res = array();
                    $cmd = $this->pngFilterCmd . " $this->cacheFileName";
                    exec($cmd, $res);
                    $this->Log($cmd);
                    $this->Log($res);
                }

                // Use external program to deflate PNG, if defined
                if ($this->pngDeflateCmd) {
                    if ($this->verbose) {
                        clearstatcache();
                        $this->Log("Filesize before deflate optimize: " . filesize($this->cacheFileName) . " bytes.");
                    }
                    $res = array();
                    $cmd = $this->pngDeflateCmd . " $this->cacheFileName";
                    exec($cmd, $res);
                    $this->Log($cmd);
                    $this->Log($res);
                }
                break;
        }

        if ($this->verbose) {
            clearstatcache();
            $this->log("Saved image to cache.");
            $this->log(" Cached image filesize: " . filesize($this->cacheFileName) . " bytes.");
            $this->log(" imageistruecolor() : " . (imageistruecolor($this->image) ? 'true' : 'false'));
            $this->log(" imagecolorstotal() : " . imagecolorstotal($this->image));
            $this->log(" Number of colors in image = " . $this->ColorsTotal($this->image));
            $index = imagecolortransparent($this->image);
            $this->log(" Detected transparent color = " . ($index > 0 ? implode(", ", imagecolorsforindex($this->image, $index)) : "NONE") . " at index = $index");
        }

        return $this;
    }
1208774
public function blend($overwrite = false)
    {
        if ($this->type == 'blend') {
            /** @var \LCI\Blend\Blendable\Blendable $currentVersion */
            $currentVersion = $this->getCurrentVersion();
            $currentVersion
                ->setRelatedData($this->related_data)
                ->makeHistory();
        }

        $this->modx->invokeEvent(
            'OnBlendBeforeSave',
            [
                'blender' => $this->blender,
                'blendable' => $this,
                'data' => &$this->blendable_xpdo_simple_object_data,
                'type' => $this->type,
                'xPDOClass' => $this->xpdo_simple_object_class,
                'xPDOSimpleObject' => &$this->xPDOSimpleObject
            ]
        );

        $save = $this->save($overwrite);

        if ($save) {
            $this->modx->invokeEvent(
                'OnBlendAfterSave',
                [
                    'blender' => $this->blender,
                    'blendable' => $this,
                    'data' => &$this->blendable_xpdo_simple_object_data,
                    'type' => $this->type,
                    'xPDOClass' => $this->xpdo_simple_object_class,
                    'xPDOSimpleObject' => &$this->xPDOSimpleObject
                ]
            );
        }

        return $save;
    }
723565
protected function write($contents, $assetFileName)
    {
        // Derive the filename that we will be writing to
        $directory = $this->getOutputDirectory($assetFileName);

        // Build the file name
        $filename = self::getOutputFileName($assetFileName);

        // The full file path
        $fullpath = $directory.$filename;

        // Make sure that the folder exists
        if(!file_exists($directory))
        {
            // It doesn't, so make it (allow us to write)
            mkdir($directory, 0777, true);
        }

        // Write the file to disk
        file_put_contents($fullpath, $contents);

        // Return the newly created file name
        return $fullpath;
    }
988084
public function writeCache()
    {
        $this->getFileSystem()->put($this->getCachePath(), $this->serializedInformationForCache($this->information));

        return $this;
    }
1233580
public function run($content, $write=false)
    {
        $this->summary = [];
        $this->kirby->cache()->flush();
        $level = 1;

        if ($write) {
            // Kill PHP Error reporting when building pages, to "catch" PHP errors
            // from the pages or their controllers (and plugins etc.). We're going
            // to try to hande it ourselves
            $level = error_reporting();
            $this->shutdown = function () {
                $this->showFatalError();
            };
            register_shutdown_function($this->shutdown);
            error_reporting(0);
        }

        // Empty folder on full site build
        if ($write && $content instanceof Site) {
            $folder = new Folder($this->outputdir);
            $folder->flush();
        }

        // Build each page (possibly several times for multilingual sites)
        foreach($this->getPages($content) as $page) {
            $this->buildPage($page, $write);
        }

        // Copy assets after building pages (so that e.g. thumbs are ready)
        if ($content instanceof Site) {
            foreach ($this->assets as $from=>$to) {
                $this->copyAsset($from, $to, $write);
            }
        }

        // Restore error reporting if building pages worked
        if ($write) {
            error_reporting($level);
            $this->shutdown = function () {};
        }
    }
400663
public function createFile($filepath, $script, $rewrite = true)
    {
        $this->createDirectory(dirname($filepath));
        
        if (!file_exists($filepath) || $rewrite === true) {
            file_put_contents($filepath, $script);
        }
        
        return $this;
    }
785943
function store( $sub_dir = false, $suffix = false, $mimeData = false )
    {
        if ( !$this->IsTemporary )
        {
            eZDebug::writeError( "Cannot store non temporary file: " . $this->Filename,
                                 "eZHTTPFile" );
            return false;
        }
        $this->IsTemporary = false;

        $ini = eZINI::instance();
//         $storage_dir = $ini->variable( "FileSettings", "VarDir" ) . '/' . $ini->variable( "FileSettings", "StorageDir" );
        $storage_dir = eZSys::storageDirectory();
        if ( $sub_dir !== false )
            $dir = $storage_dir . "/$sub_dir/";
        else
            $dir = $storage_dir . "/";
        if ( $mimeData )
            $dir = $mimeData['dirpath'];

        if ( !$mimeData )
        {
            $dir .= $this->MimeCategory;
        }

        if ( !file_exists( $dir ) )
        {
            if ( !eZDir::mkdir( $dir, false, true ) )
            {
                return false;
            }
        }

        $suffixString = false;
        if ( $suffix != false )
            $suffixString = ".$suffix";

        if ( $mimeData )
        {
            $dest_name = $mimeData['url'];
        }
        else
        {
            $dest_name = $dir . "/" . md5( basename( $this->Filename ) . microtime() . mt_rand() ) . $suffixString;
        }

        if ( !move_uploaded_file( $this->Filename, $dest_name ) )
        {
            eZDebug::writeError( "Failed moving uploaded file " . $this->Filename . " to destination $dest_name" );
            unlink( $dest_name );
            $ret = false;
        }
        else
        {
            $ret = true;
            $this->Filename = $dest_name;
            $perm = $ini->variable( "FileSettings", "StorageFilePermissions" );
            $oldumask = umask( 0 );
            chmod( $dest_name, octdec( $perm ) );
            umask( $oldumask );

            // Write log message to storage.log
            $storageDir = $dir . "/";
            eZLog::writeStorageLog( basename( $this->Filename ), $storageDir );
        }
        return $ret;
    }
1191993
private function fileMove($overwrite = true)
    {
        if (!$this->file->getLocation() || $overwrite) {
            $origin = $this->file->getTmp();

            // This will typically be /media/files
            $uploadDir = realpath($this->coreParametersHelper->getParameter('upload_dir'));
            $fileName  = $this->getFileName(
                $this->file->getCompression()
            );
            $target    = $uploadDir.'/client_payloads/'.$this->contactClient->getId().'/'.$fileName;
            if ($origin && (!file_exists($target) || $overwrite)) {
                $this->filesystemLocal->copy($origin, $target, $overwrite);
                if (file_exists($target)) {
                    // Final file name as it will be seen by the client.
                    $this->file->setName($fileName);
                    $this->setLogs($fileName, 'fileName');

                    $this->file->setDateAdded(new \DateTime());

                    $this->file->setLocation($target);
                    $this->setLogs($target, 'fileLocation');

                    $crc32 = hash_file('crc32b', $target);
                    $this->file->setCrc32($crc32);
                    $this->setLogs($crc32, 'crc32');

                    $md5 = hash_file('md5', $target);
                    $this->file->setMd5($md5);
                    $this->setLogs($md5, 'md5');

                    $sha1 = hash_file('sha1', $target);
                    $this->file->setSha1($sha1);
                    $this->setLogs($sha1, 'sha1');

                    $this->setLogs(filesize($target), 'fileSize');

                    $this->filesystemLocal->remove($origin);

                    $this->file->setStatus(File::STATUS_READY);
                    $this->setLogs($this->file->getStatus(), 'fileStatus');
                } else {
                    throw new ContactClientException(
                        'Could not move file to local location.',
                        Codes::HTTP_INTERNAL_SERVER_ERROR,
                        null,
                        Stat::TYPE_ERROR,
                        false
                    );
                }
            }
        }

        return $this;
    }
1278016
public function write(): string
    {
        $this->targetFile->removeIfExists();

        return $this->fileWriter->write($this->targetFile);
    }
983314
public function writeFile()
    {
        if (!empty($this->fileName)) {
            if (!file_exists($this->destinationPath)) {
                mkdir($this->destinationPath, 0777);
            }
            $file = $this->file();
            $fileHandle = fopen($file, 'w+');
            fwrite($fileHandle, $this->body);
            fclose($fileHandle);
            return $file;
        } else {
            return FALSE;
        }
    }
1352383
public function encodeFile( $fileName, $overwrite = FALSE )
	{
		if( !file_exists( $fileName ) )
			return FALSE;
		if( $this->isEncoded( $fileName ) )
			return TRUE;
		$php		= FS_File_Reader::load( $fileName );
		$code		= $this->encode( $php );
		$dirname	= dirname( $fileName );
		$basename	= basename( $fileName );
		$target		= $dirname."/".$this->filePrefix.$basename.$this->fileSuffix;
		if( $fileName == $target && !$overwrite )
			throw new RuntimeException( 'File cannot be overwritten, use Parameter "overwrite".' );
//		copy( $fileName, "#".$fileName );
		return (bool) FS_File_Writer::save( $target, $code );
	}
386507
public function assetsFolderUrlToBeWritten() {
        $url = null;

        //the default rules only require the object to have an ID
        //but more sophisticated rules might require more - e.g. a title to be set
        //thus we check if the object is ready for folder creation - if custom rules
        //(UploadDirRulesInterface) have been set
        if (method_exists($this->owner, 'getReadyForFolderCreation')) {
            if (!$this->owner->getReadyForFolderCreation()) {
                return false;
            }
        }

        //check if the page we're having is implementing the UploadDirRulesInterface
        //for rule customization
        if ($this->owner instanceof UploadDirRulesInterface) {
            $url = $this->owner->getCalcAssetsFolderDirectory();
        } else {
            //else use the default settings

            $class = UploadDirRules::get_rules_class();
            $url = $class::calc_full_directory_for_object($this->owner);
        }
        return $url;
    }
1357918
public function writeFile($overwrite = false) {
		$overwrite = ($this->type == self::TYPE_ENTITY) ? false : $overwrite;
		if (($this->existsFile() < self::FILE_MATCH) || $overwrite) {
			//$checkPath = dirname($this->getPath());
			if (@file_put_contents($this->getPath(), $this->getCode(), LOCK_EX)) {
				@chmod($this->getPath(), 0666);
				return true;
			}/* else {
				throw new Exception\PermissionException('Write access to Entity OR Model dirs denied');
			}*/
		}
		return false;
	}
267889
public function create()
    {
        $dir = $this->Folder->pwd();

        if (is_dir($dir) && is_writable($dir) && !$this->exists() && touch($this->path)) {
            return true;
        }

        return false;
    }
1364858
public function move(Filesystem $filesystem = null, $targetFilename = null, $overwrite = true)
    {
        // source file
        $sourceFile = $this->getTransport()->getFile();
        
        // target base name
        $targetBasename = $this->buildTargetBasename($sourceFile, $targetFilename);

        // move file to target storage
        $content = stream_get_contents($sourceFile->getStream());

        // write file
        $filesystem->write(
            $targetBasename,
            $content,
            $overwrite
        );

        return $filesystem->get($targetBasename);
    }