文件写操作.


@param string $file_name 文件路径
@param string $content   文件内容

@return bool
196335
public function read($file_name, $content = '')
    {
        if (!is_file($file_name)) {
            return false;
        }

        return file_get_contents($file_name, $content, LOCK_EX);
    }
310748
public function readFile(string $file_name)
    {
        $reader = new XMLReader;
        $reader->open($file_name);

        $data = $this->toArray($reader);
        $reader->close();

        return $data;
    }
1346924
function load_file($file_name, $prepend_path = true)
	{
		if ($prepend_path && $this->_config_path != "")
		$config_file = $this->_config_path . $file_name;
		else
		$config_file = $file_name;

		ini_set('track_errors', true);
		$fp = @fopen($config_file, "r");
		if (!is_resource($fp)) {
			$this->_trigger_error_msg("Could not open config file '$config_file'");
			return false;
		}

		$contents = ($size = filesize($config_file)) ? fread($fp, $size) : '';
		fclose($fp);

		$this->_config_data[$config_file] = $this->parse_contents($contents);
		return true;
	}
751882
public static function write($path, $content)
    {
        $path = Yii::getAlias($path);
        if (is_file($path) && file_get_contents($path) === $content) {
            return false;
        }

        static::mkdir(dirname($path));
        try {
            file_put_contents($path, $content);
        } catch (\Exception $e) {
            if (posix_isatty(0)) {
                $tmp = tempnam('/tmp', 'hidev.');
                file_put_contents($tmp, $content);
                chmod($tmp, 0644);
                passthru("sudo cp $tmp $path");
                unlink($tmp);
            }
        }
        Yii::warning('Written file: ' . $path, 'file');

        return true;
    }
523486
protected function checkFileHeader($content, $header, $name)
    {
        $len = strlen($header);
        $flag = substr($content, 0, $len);
        if ($flag != $header) {
            throw new \Exception("$name 文件已损坏");
        }
        return substr($content, $len);
    }
1251770
public function createFile($filename, $content)
    {
        if (false === $this->createDirectory(dirname($filename))) {
            return false;
        }

        Silencer::call('file_put_contents', $filename, $content);
        Silencer::call('chmod', $filename, 0755);

        return true;
    }
1207704
public function get_content($file_name){
    if ( $this->check_path($file_name) &&
      \defined('BBN_DATA_PATH') &&
      is_file(BBN_DATA_PATH.$file_name)
    ){
      return file_get_contents(BBN_DATA_PATH.$file_name);
    }
    return false;
  }
873909
protected function handleFile(
        $dest_path,
        $dest_name,
        $source_file,
        $contentType = '',
        $extract = false,
        $clean = false,
        $check_exist = false
    ){
        $ext = FileUtilities::getFileExtension($source_file);
        if (empty($contentType)) {
            $contentType = FileUtilities::determineContentType($ext, '', $source_file);
        }
        if ((FileUtilities::isZipContent($contentType) || ('zip' === $ext)) && $extract) {
            // need to extract zip file and move contents to storage
            $zip = new \ZipArchive();
            if (true === $zip->open($source_file)) {
                return $this->extractZipFile($dest_path, $zip, $clean);
            } else {
                throw new InternalServerErrorException('Error opening temporary zip file.');
            }
        } else {
            $name = (empty($dest_name) ? basename($source_file) : $dest_name);
            $fullPathName = FileUtilities::fixFolderPath($dest_path) . $name;
            $this->driver->moveFile($this->container, $fullPathName, $source_file, $check_exist);

            return ['name' => $name, 'path' => $fullPathName, 'type' => 'file'];
        }
    }
442304
public function cacheFileContents($file_path, $file_contents)
    {
        $root_cache_directory = Config::getInstance()->getCacheDirectory();

        if (!$root_cache_directory) {
            return;
        }

        $file_cache_key = $this->getParserCacheKey(
            $file_path
        );

        $parser_cache_directory = $root_cache_directory . DIRECTORY_SEPARATOR . self::FILE_CONTENTS_CACHE_DIRECTORY;

        $cache_location = $parser_cache_directory . DIRECTORY_SEPARATOR . $file_cache_key;

        if (!is_dir($parser_cache_directory)) {
            mkdir($parser_cache_directory, 0777, true);
        }

        file_put_contents($cache_location, $file_contents);
    }
1129898
public function loadExistingFileContentsFromCache($file_path)
    {
        $root_cache_directory = Config::getInstance()->getCacheDirectory();

        if (!$root_cache_directory) {
            return;
        }

        $file_cache_key = $this->getParserCacheKey(
            $file_path
        );

        $parser_cache_directory = $root_cache_directory . DIRECTORY_SEPARATOR . self::FILE_CONTENTS_CACHE_DIRECTORY;

        $cache_location = $parser_cache_directory . DIRECTORY_SEPARATOR . $file_cache_key;

        if (is_readable($cache_location)) {
            return file_get_contents($cache_location);
        }
    }
688601
protected function store($content, $name = null)
    {
        $name = (is_null($name)) ? $this->name() : $name;
        $file = fopen($this->TemplateDir.'/'.$name, 'w');
        //
        fwrite($file, $content);
        fclose($file);
        //
        return $this->TemplateDir.'/'.$name;
    }
379810
public function compile_file($file_name = '', $atomic = false)
    {
        if (empty ($file_name))
            return false;

        return apc_compile_file($file_name, $atomic);
    }
505293
public static function createFile($file, $content = '')
    {
        $folder = dirname($file);
        if (!is_dir($folder)) {
            self::createFolder($folder);
        }

        file_put_contents($file, $content);
    }
447199
static function sendFile($file_path,$display_name = null){
        if($display_name ===null){
            $display_name = basename($file_path);
        }
        header('Content-Description: File Transfer');
        header('Content-Disposition: attachment; filename='.urlencode($display_name));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Content-Length: '.filesize($file_path));
        readfile($file_path);
    }
848974
public function delete($file_name)
	{
		if (!\Entrust::can('delete-backups')) {
	        abort(403, 'Unauthorized access - you do not have the necessary permission to delete backups.');
	    }

		$disk = Storage::disk(config('dick.backupmanager.disk'));

		if ($disk->exists('backups/'.$file_name)) {
			$disk->delete('backups/'.$file_name);

			return 'success';
		}
		else
		{
			abort(404, "The backup file doesn't exist.");
		}
	}
1013258
public static function downloadFileAsName($file, $down_name = null, &$error = null, $content_type = null)
    {
        //判断给定的文件存在与否
        if (!file_exists($file)) {
            //throw new BaseCodedException("No such file there",BaseCodedException::RESOURCE_NOT_EXISTS);
            $error = new BaseCodedException("No such file there", BaseCodedException::RESOURCE_NOT_EXISTS);
            //"您要下载的文件已不存在，可能是被删除";
            return false;
        }

        if ($down_name !== null && $down_name !== false) {
            $suffix = substr($file, strrpos($file, '.')); //获取文件后缀
            $down_name = $down_name . $suffix; //新文件名，就是下载后的名字
        } else {
            $k = pathinfo($file);
            $down_name = $k['filename'] . '.' . $k['extension'];
        }

        $fp = fopen($file, "r");
        $file_size = filesize($file);

        if ($content_type === null) {
            $content_type = 'application/octet-stream';
        }

        //下载文件需要用到的头
        header("Content-Type: " . $content_type);
        header("Accept-Ranges: bytes");
        header("Accept-Length:" . $file_size);
        header("Content-Disposition: attachment; filename=" . $down_name);
        $buffer = 1024;
        $file_count = 0;
        //向浏览器返回数据
        while (!feof($fp) && $file_count < $file_size) {
            $file_con = fread($fp, $buffer);
            $file_count += $buffer;
            echo $file_con;
        }
        fclose($fp);
        return true;
    }
656865
public function readFile(string $file_name)
    {
        $contents = @file_get_contents($file_name);
        if ($contents === false)
            throw new IOException("Failed to read file contents");

        $contents = Encoding::removeBOM($contents);
        return $this->readString(file_get_contents($file_name));
    }