Add headers to a specific file
@param  string $filename The temporary filename while the file is being written
966140
protected function addHeader($filename)
    {
        $file = fopen($filename, 'w');
        $bom = pack("CCC", 0xef, 0xbb, 0xbf);
        fwrite($file, $bom);

        $name = $this->getName();
        if (isset($this->data[$name], $this->data[$name]['format'], $this->data[$name]['format'][0]) && in_array('addHeader', $this->data[$name]['format'])) {
            $labeledCols = $this->getLabeledColumns();
            $labels      = array();

            if (in_array('formatVariable', $this->data[$name]['format'])) {
                foreach($labeledCols as $columnName) {
                    $labels[] = $this->model->get($columnName, 'label');
                }
            } else {
                $labels = $labeledCols;
            }

            if (isset($this->data[$name]) && isset($this->data[$name]['delimiter'])) {
                $this->delimiter = $this->data[$name]['delimiter'];
            }

            fputcsv($file, $labels, $this->delimiter, '"');
        }

        fclose($file);
    }
155187
public function addFile($filename)
    {
        if ($this->filename === null) {
            $this->filename = basename($filename);
        }

        $stream = fopen($filename, 'r+');
        if ($stream === false) {
            throw new InvalidParamException("Unable to read file '{$filename}'");
        }
        return $this->addStream($stream);
    }
1915
public function save($fileName = null)
	{
		file_put_contents(empty($fileName) ? $this->fileName : $fileName, json_encode($this->data), LOCK_EX);
	}
401413
public function addHeader($header, $value)
    {
        $name = strtolower($header);

        if(is_array($value))
        {
            foreach($value as $v)
                $this->headers[$name][] = trim($v);

            foreach(array_keys($this->headerLines) as $key)
                if(strtolower($key) == $name)
                    unset($this->headerLines[$key]);

            $this->headerLines[$header] = $this->headers[$name];
        }
        else
        {
            $this->headers[$name][] = trim($value);

            foreach(array_keys($this->headerLines) as $key)
                if(strtolower($key) == $name)
                    unset($this->headerLines[$key]);

            $this->headerLines[$header] = $this->headers[$name];
        }

        return $this;
    }
179802
public function setFileName($fileName)
    {
        $this->fileName = $fileName;
        $this->headers->add(array('Content-Disposition' => 'attachment; filename="' . $this->fileName . '"'));
        return $this;
    }
626978
public function write($filename)
    {
        if (count($this) == 0) return;
        $file = fopen($filename, 'w');
        fwrite($file, $this->toJson());
        fclose($file);
        unset($file);
    }
1135914
protected function setFilenameOption($filename): void
    {
        if (is_string($filename)) {
            $filename = new BasicFilename($filename);
        }

        if (!is_callable($filename)) {
            throw new TypeError("Filename should be a string or callable");
        }

        $this->filename = $filename;
    }
1227543
def push_header(self, filename):
        
        # open file and read it all
        with open(filename, "r") as infile:
            content = infile.read()

        # push header
        content = self.__header + content

        # re-write file with the header
        with open(filename, "w") as outfile:
            outfile.write(content)
725402
public function appendToFile($filename, $content)
    {
        $dir = dirname($filename);

        if (!is_dir($dir)) {
            $this->mkdir($dir);
        }

        if (!is_writable($dir)) {
            throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir), 0, null, $dir);
        }

        if (false === @file_put_contents($filename, $content, FILE_APPEND)) {
            throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
        }
    }
882536
private function _writeToFile($filename, $contents)
    {
        // Check local tmp directory
        if (!is_dir($this->config['tmp']) || !is_writeable($this->config['tmp'])) {
            $this->fail('tmp directory not found or is not writable');
        }

        // Build temp file
        $tmp_file = $this->config['tmp'] . "/ftp_data_file.tmp";
        file_put_contents($tmp_file, $contents);

        // Update variables
        $this->filepath = $filename;
        $this->file = $contents;

        // Upload the file to server
        if ($this->isSFTP()) {
            $flag = defined('NET_SFTP_LOCAL_FILE') ? NET_SFTP_LOCAL_FILE : \phpseclib\Net\SFTP::SOURCE_LOCAL_FILE;

            $uploaded = @$this->ftp->put($filename, $tmp_file, $flag);
        } else {
            $uploaded = ftp_put($this->ftp, $filename, $tmp_file, FTP_BINARY);
        }
        if (!$uploaded) {
            $this->fail('failed to upload file to server');
        }
    }
337825
protected function addSpssFile($filename)
    {
        $model       = $this->model;
        $files       = $this->getFiles();
        $datFileName = array_search($filename, $files);
        $spsFileName = substr($datFileName, 0, -strlen($this->fileExtension)) . '.sps';
        $tmpFileName = substr($filename, 0, -strlen($this->fileExtension)) . '.sps';

        $this->files[$spsFileName] = $tmpFileName;
        if ($this->batch) {
            $this->batch->setSessionVariable('files', $this->files);
        } else {
            $this->_session->files = $this->files;
        }
        $this->addHeader($tmpFileName);
        $file = fopen($tmpFileName, 'a');

        //first output our script
        fwrite($file,
            "SET UNICODE=ON.\n" .
            "SHOW LOCALE.\n" .
"PRESERVE LOCALE.\n" .
"SET LOCALE='en_UK'.\n\n" .
"GET DATA\n" .
" /TYPE=TXT\n" .
" /FILE=\"" . $datFileName . "\"\n" .
" /DELCASE=LINE\n" .
" /DELIMITERS=\"".$this->delimiter."\"\n" .
" /QUALIFIER=\"'\"\n" .
" /ARRANGEMENT=DELIMITED\n" .
" /FIRSTCASE=1\n" .
" /IMPORTCASE=ALL\n" .
" /VARIABLES=");


        $labeledCols = $this->getLabeledColumns();
        $labels      = array();
        $types       = array();
        $fixedNames  = array();
        //$questions  = $survey->getQuestionList($language);
        foreach ($labeledCols as $colname) {

            $fixedNames[$colname] = $this->fixName($colname);
            $options          = array();
            $type             = $model->get($colname, 'type');
            switch ($type) {
                case \MUtil_Model::TYPE_DATE:
                    $type = 'SDATE10';
                    break;

                case \MUtil_Model::TYPE_DATETIME:
                    $type = 'DATETIME23';
                    break;

                case \MUtil_Model::TYPE_TIME:
                    $type = 'TIME8.0';
                    break;

                case \MUtil_Model::TYPE_NUMERIC:
                    $defaultSize = $this->defaultNumericSize;
                    $type        = 'F';
                    break;

                //When no type set... assume string
                case \MUtil_Model::TYPE_STRING:
                default:
                    $defaultSize = $this->defaultAlphaSize;
                    $type        = 'A';
                    break;
            }
            $types[$colname] = $type;
            if ($type == 'A' || $type == 'F') {
                $size = $model->get($colname, 'maxlength');   // This comes from db when available
                if (is_null($size)) {
                    $size = $model->get($colname, 'size');    // This is the display width
                    if (is_null($size)) {
                        $size = $defaultSize;                   // We just don't know, make it the default
                    }
                }
                if ($type == 'A') {
                    $type = $type . $size;
                } else {
                    $type = $type . $size . '.' . ($size - 1);    //decimal
                }
            }
            //if (isset($questions[$colname])) {
            //    $labels[$colname] = $questions[$colname];
            //}
            fwrite($file, "\n " . $fixedNames[$colname] . ' ' . $type);
        }
        fwrite($file, ".\nCACHE.\nEXECUTE.\n");
        fwrite($file, "\n*Define variable labels.\n");
        foreach ($labeledCols as $colname) {

            $label = "'" . $this->formatString($model->get($colname, 'label')) . "'";
            fwrite($file, "VARIABLE LABELS " . $fixedNames[$colname] . " " . $label . "." . "\n");
        }

        fwrite($file, "\n*Define value labels.\n");
        foreach ($labeledCols as $colname) {
            if ($options = $model->get($colname, 'multiOptions')) {
                fwrite($file, 'VALUE LABELS ' . $fixedNames[$colname]);
                foreach ($options as $option => $label) {
                    $label = "'" . $this->formatString($label) . "'";
                    if ($option !== "") {
                        if ($types[$colname] == 'F') {
                            //Numeric
                            fwrite($file, "\n" . $option . ' ' . $label);
                        } else {
                            //String
                            fwrite($file, "\n" . '"' . $option . '" ' . $label);
                        }
                    }
                }
                fwrite($file, ".\n\n");
            }
        }

        fwrite($file, "RESTORE LOCALE.\n");

        fclose($file);
    }
824208
public function setFileName(string $fileName): self
    {
        if (!preg_match('#.pdf$#i', $fileName)) {
            $fileName .= '.pdf';
        }

        $this->fileName = System::getContainer()->get('huh.utils.file')->sanitizeFileName($fileName);

        return $this;
    }
1341803
public function setCompileFile($filename)
    {
        $filename = (($this->compileFileNameMd5) ? md5(md5($filename)) : $filename);
        $this->templateCompileFile = $this->compilePath . $filename . $this->compileFileSuffix;
    }
656101
public function setFilename($filename)
    {
        if (is_array($filename) && isset($filename['name'])) {
            $this->filename = $filename['name'];
        } else {
            $this->filename = $filename;
        }
    }
548117
private static function addConfigFromFile($filename)
    {
        $file_path = self::$default_dir . $filename . ".php";

        if (! file_exists($file_path)) {
            $err_msg = "The file '" . str_replace(['\\','/'], DIRECTORY_SEPARATOR, $file_path) . "' is not found!";
            throw new \Exception($err_msg);
        }

        self::$settings[$filename] = new \Phalcon\Config\Adapter\Php($file_path);
    }
84341
public function addFile($filename)
    {
        if (file_exists($this->path . $filename) && preg_match('/\.' . $this->type . '$/i', $filename)) {
            $this->files[] = $filename;
        } elseif (!empty($this->domains)) {
            foreach ($this->domains as $domain => $paths) {
                $domainFilename = str_replace($domain, $paths["public"], $filename);
                if (file_exists($domainFilename) && preg_match('/\.' . $this->type . '$/i', $domainFilename)) {
                    $this->files[] = $domainFilename;
                }
            }
        }
        return $this;
    }
673620
public function addHeader($name, $value)
    {
        Validate::isString($name, 'name');
        Validate::isString($value, 'value');

        $this->_headers[$name] = $value;
    }