/*
输出为excel文件

@author leo<leo19920823@gmail.com>

@param $data array 需要保存的数据 $data[行号][列名]
@param $filename string 需要输出的文件名
@param $save_path string 默认是输出到浏览器，填写路径末尾加/

@return 0
557845
public static function arrSaveCsv($arr, $filename = 'out.csv', $save_path = false)
    {
        try {
            $str = '';
            foreach ($arr as $v) {
                $str1 = '';
                if (is_array($v)) {
                    foreach ($v as &$vv) {
                        if (is_array($vv)) {
                            $vv = implode('|',$vv);
                        }
                    }
                    $str1 = implode(',', $v);
                } elseif (is_string($v)) {
                    $str1 = $v;
                }
                if (!empty($str1)) {
                    $str .= "{$str1}\n";
                }
            }

            if (false === $save_path) {
                header('Content-Type: text/csv');
                header("Content-Disposition: attachment;filename={$filename}.csv");
                header('Cache-Control: max-age=0');
                echo $str;
                return 0;
            } else {
                if(!is_dir($save_path)){
                    mkdir($save_path,0777,true);
                }
                if (false == file_put_contents("{$save_path}{$filename}.csv",$str)) {
                    throw new Exception();
                }
                return 0;
            }

        } catch (\Exception $e) {
            throw $e;
        }
    }
191877
public function saveFile($data, $file)
    {
        $openfile = @fopen($file, "wb") or die("saveFile: Error opening file '{$file}'.");
        @fwrite($openfile, $data) or die("saveFile: Error writing data into file '{$file}'.");
        @fclose($openfile);
    }
994089
private function putFile($path, $data)
    {
        $info = false;
        $url = parse_url($path);

        $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 5);
        if (!$fp) {
            return false;
        }

        $buffer = '';
        $b = "\r\n";

        stream_set_blocking($fp, true);
        stream_set_timeout($fp, 30, 200000);

        $out = 'PUT '.$url['path'].' HTTP/1.1'.$b;
        $out .= 'Host: '.$url['host'].$b;
        $out .= 'Content-Length: '.Util\Size::fromContent($data).$b.$b;
        $out .= $data;
        $out .= $b.$b;
        fwrite($fp, $out);
        fflush($fp);

        stream_set_blocking($fp, true);
        stream_set_timeout($fp, 30, 200000);

        while (!feof($fp) && !$info['timed_out']) {
            $info = stream_get_meta_data($fp);
            $buffer .= fgets($fp, 128);
        }
        fclose($fp);

        return true;
    }
653276
public function save( string $name, $data, int $length ): bool
	{
		if ($length <= 0)
			throw new SettingsException("Expiration time has to be greater than 0.");

		$path = $this->cacheDir . DIRECTORY_SEPARATOR . $name;
		$res  = @fopen($path, 'w+');

		if ($res == false)
			throw new SettingsException("Saving - Cache file ($path) failed to be opened/created.");

		$storage = new FileCacheStorage($data, $length);
		$written = fwrite($res, serialize($storage));
		fclose($res);

		return boolval($written);
	}
1323657
protected function write_data(string $path, string $data) {
        $targetpath = $this->path . DIRECTORY_SEPARATOR . $path;
        check_dir_exists(dirname($targetpath), true, true);
        if (file_put_contents($targetpath, $data) === false) {
            throw new \moodle_exception('cannotsavefile', 'error', '', $targetpath);
        }
        $this->files[$path] = $targetpath;
    }
810914
public static void create(String data, String fileName, int width,
                              int height) throws IOException {
        create(data, new FileOutputStream(fileName), width, height);
        logger.debug("二维码保存位置为：{}", fileName);
    }
456930
def write_file(file_path, gd_data)
      if gd_data && !File.exists?(file_path)
        FileUtils.mkdir_p(File.dirname(file_path))
        File.open(file_path, "wb") do |data|
          data << gd_data
        end
      end
    end
978022
public static function getFileNameSave($file_name)
    {
        FileHelper::createDirectory(self::getUploadDir() . '/' . date('Y/m/d'));
        return self::getUploadDir() . '/' . $file_name;
    }
1353527
public function excel($filename, $data)
    {
        \Excel::create($filename, function ($excel) use ($filename, $data) {
            $excel->sheet($filename, function ($sheet) use ($data) {
                $sheet->fromArray($data);
            });
        })
              ->export('xlsx');
    }
876509
public static function saveJSON($data, $filename, $jsonFlag = 0)
    {
        $content = json_encode($data, $jsonFlag);
        self::save($filename, $content);
    }
1301306
public static function save( $fileName, $data )
	{
		$yaml	= FS_File_YAML_Spyc::YAMLDump( $data );
		return FS_File_Writer::save( $fileName, $yaml );
	}
787555
public static function save(string $filename, string $data): bool
    {
        return \file_put_contents($filename, $data) !== false;
    }
760486
public function open($save_path, $name)
    {
        if (is_null($this->handlers)) {
            $this->seedHandlerStack();
        }

        /** @var callable $start */
        $start = $this->stacks['create']->top();
        $this->handlerLock = true;
        $data = $start($save_path, $name);
        $this->handlerLock = false;
        return $data;
    }
356203
def _save_file(self, data, path):
        
        with open(path, 'wb') as tfile:
            for chunk in data:
                tfile.write(chunk)
169033
public function save(string $path, string $name, ?string $extension = null, int $quality = 100): bool
    {
        if (false === is_dir($path)) {
            mkdir($path, 0777, true);
        }
        if (null === $extension) {
            /** @var string $extension */
            $extension = $this->extension;
        }
        if (true === $this->encrypted) {
            file_put_contents($path . $name . '.' . self::FORMAT_WD_IMAGE, $this->workingData);
            return true;
        } elseif (0 !== imagetypes()) {
            $filename = $path . $name . '.' . $extension;
            switch (strtolower($extension)) {
                case self::FORMAT_GIF:
                    imagegif($this->workingData, $filename);
                    break;
                case self::FORMAT_JPEG:
                case self::FORMAT_JPG:
                    imagejpeg($this->workingData, $filename, $quality);
                    break;
                case self::FORMAT_PNG:
                    imagepng($this->workingData, $filename, (int)round(9 - ((9 * $quality) / 100)));
                    break;
                default:
                    throw new ImageException('Unsupported file type');
            }
            return false;
        }
    }
825248
protected function _encodeFile( $fileName, $data )
    {
        $_filePath = dirname( $fileName );

        if ( !is_dir( $_filePath ) )
        {
            if ( false === @mkdir( $_filePath, 0777, true ) && !is_dir( $_filePath ) )
            {
                return false;
            }
        }
        elseif ( !is_writable( $_filePath ) )
        {
            return false;
        }

        $_tempFile = tempnam( $_filePath, basename( $fileName ) );

        if ( false !== file_put_contents( $_tempFile, json_encode( $data ) ) && @rename( $_tempFile, $fileName ) )
        {
            @chmod( $fileName, 0666 & ~umask() );

            return true;
        }

        return false;
    }
836879
public static function createJson($strPath, $strFileName, array $data = [])
    {
        $cleanPath = self::cleanPath($strPath);

        if (!file_exists($strPath)) {
            mkdir($strPath, 0775);
        }

        $pathFile = $cleanPath . self::DS . $strFileName . '.json';

        if (!file_exists($pathFile)) {
            $file = fopen($pathFile, 'w+');
            fwrite($file, json_encode($data));

            return fclose($file);
        }

        return null;
    }