PHP中fwrite与file_put_contents的区别

 更新时间:2016年11月25日 15:31  点击:1648
PHP中fwrite与file_put_contents在细节上在区别但在性能上file_put_contents更强大了,我们下面举例来对比一下吧。

相同点:file_put_contents() 函数把一个字符串写入文件中,与依次调用 fopen(),fwrite() 以及 fclose() 功能一样。

不同点:在file_put_contents()函数中使用 FILE_APPEND 可避免删除文件中已有的内容,即实现多次写入同一个文件时的追加功能。

例如: 

echo file_put_contents("test.txt","Hello World. Testing!",FILE_APPEND);

file_put_contents是以追加的形式将字符串写入到test.txt中,

fwrtie则是会清除之前的记录,只保留当前写入的内容

$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);

file_put_contents代替fwrite优点多多

如下为file_put_contents的实例代码:

<?php
$filename = 'file.txt';
$word = "你好!\r\nwebkaka";  //双引号会换行 单引号不换行
file_put_contents($filename, $word);
?>

同样的功能使用fwrite的实例代码:

<?php
$filename = 'file.txt';
$word = "你好!\r\nwebkaka";  //双引号会换行  单引号不换行
$fh = fopen($filename, "w"); //w从开头写入 a追加写入
echo fwrite($fh, $word);
fclose($fh);
?>

从以上两个例子看出,其实file_put_contents是fopen、fwrite、fclose三合一的简化写法,这对程序代码的优化是有好处的,一方面在代码量上有所减少,另一方面不会出现fclose漏写的不严密代码,在调试、维护上方便很多。

上述例子里,file_put_contents是从头写入,如果要追加写入,怎么办呢?

在file_put_contents的语法里,有个参数FILE_APPEND,这是追加写入的声明。实例代码如下:

<?php
echo file_put_contents('file.txt', "This is another something.", FILE_APPEND);
?>

FILE_APPEND就是追加写入的声明。在追加写入时,为了避免其他人同时操作,往往需要锁定文件,这时需要加多一个LOCK_EX的声明,写法如下:

<?php
echo file_put_contents('file.txt', "This is another something.", FILE_APPEND|LOCK_EX);
?>

注意,以上代码中echo输出到显示器里的是写入文件字符串的长度。

常见问题:

Warning: fopen(file.txt) [function.fopen]: failed to open stream: Permission denied

当写入文件时,有时会遇到上述问题,这是因为文件没有写权限的原因。为了避免这个错误的出现,在写入文件时需要判断下文件是否可写,这需要用到is_writable()这个函数。实例代码如下:

<?php
$filename = 'file.txt';
if (is_writable($filename)) {
echo file_put_contents($filename, "This is another something.", FILE_APPEND);
} else {
    echo "文件 $filename 不可写";
}
?>

fwrite简单的把数据写到handler里面
file_put_contents可能需要处理contenxt,数据类型为mixed,需要更多处理
虽然看file_put_contents的函数说明:和依次调用 fopen(),fwrite() 以及 fclose() 功能一样。
但是肯定有细微差别的,尤其是在重复写入大量数据的时候,file_put_contents无疑会重复的fopen,fclose .而 fwrite则可以只一次fopen,fwrite即可

写个简单程序测试一下,一个250M文件

<!--
<?php
$len = 1024*1024*25;
$data = str_repeat(“-”,$len);

$start = microtime(true);
$fp = fopen(“/tmp/b”,”w”);
fwrite($fp,$data,$len);
fclose($fp);
$end = microtime(true);
echo “elipsed time:”.($end-$start).”\n”;

$start = microtime(true);
file_put_contents(“/tmp/a”,$data);
$end = microtime(true);
echo “elipsed time:”.($end-$start).”\n”;

silver@silver-desktop:~/php$ php fwrite_VS_file_put_contents.php
elipsed time:6.0958020687103
elipsed time:9.6280250549316
silver@silver-desktop:~/php$ php fwrite_VS_file_put_contents.php
elipsed time:6.247565984726
elipsed time:9.0449070930481

-->


结论:多次执行结果类试,说明fopen,fwrite,fclose方式比直接file_put_contents要快一点!

那么为什么呢? 查看源代码

我用的ubuntu12.04
直接sudo apt-get source php5
解压:silver@silver-desktop:~/php/php5-5.3.3

查找函数fwrite 函数:silver@silver-desktop:~/php/php5-5.3.3$ grep -rn “PHP_FUNCTION(fwrite)” .
./ext/standard/file.c:1233:PHPAPI PHP_FUNCTION(fwrite)
./ext/standard/file.h:43:PHPAPI PHP_FUNCTION(fwrite);

fwrite

找到对应源码,该函数非常简单:

<!--
PHP_FUNCTION(fwrite)
{
zval *arg1;
char *arg2;
int arg2len;
int ret;
int num_bytes;
long arg3 = 0;
char *buffer = NULL;
php_stream *stream;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, “rs|l”, &arg1, &arg2, &arg2len, &arg3) == FAILURE) {
RETURN_FALSE;
}

if (ZEND_NUM_ARGS() == 2) {
num_bytes = arg2len;
} else {
num_bytes = MAX(0, MIN((int)arg3, arg2len));
}

if (!num_bytes) {
RETURN_LONG(0);
}

PHP_STREAM_TO_ZVAL(stream, &arg1);

if (PG(magic_quotes_runtime)) {
buffer = estrndup(arg2, num_bytes);
php_stripslashes(buffer, &num_bytes TSRMLS_CC);
}
ret = php_stream_write(stream, buffer ? buffer : arg2, num_bytes);
if (buffer) {
efree(buffer);
}

RETURN_LONG(ret);
}
-->

file_put_contents
该函数的处理操作就多多了


<!--
PHP_FUNCTION(file_put_contents)
{
php_stream *stream;
char *filename;
int filename_len;
zval *data;
int numbytes = 0;
long flags = 0;
zval *zcontext = NULL;
php_stream_context *context = NULL;
php_stream *srcstream = NULL;
char mode[3] = “wb”;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, “sz/|lr!”, &filename, &filename_len, &data, &flags, &zcontext) == FAILURE) {
return;
}
if (Z_TYPE_P(data) == IS_RESOURCE) {
php_stream_from_zval(srcstream, &data);
}

context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);

if (flags & PHP_FILE_APPEND) {
mode[0] = ‘a’;
} else if (flags & LOCK_EX) {
/* check to make sure we are dealing with a regular file */
if (php_memnstr(filename, “://”, sizeof(“://”) – 1, filename + filename_len)) {
if (strncasecmp(filename, “file://”, sizeof(“file://”) – 1)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, “Exclusive locks may only be set for regular files”);
RETURN_FALSE;
}
}
mode[0] = ‘c’;
}
mode[2] = ‘\0′;

stream = php_stream_open_wrapper_ex(filename, mode, ((flags & PHP_FILE_USE_INCLUDE_PATH) ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context);
if (stream == NULL) {
RETURN_FALSE;
}
if (flags & LOCK_EX && (!php_stream_supports_lock(stream) || php_stream_lock(stream, LOCK_EX))) {
php_stream_close(stream);
php_error_docref(NULL TSRMLS_CC, E_WARNING, “Exclusive locks are not supported for this stream”);
RETURN_FALSE;
}

if (mode[0] == ‘c’) {
php_stream_truncate_set_size(stream, 0);
}

switch (Z_TYPE_P(data)) {
case IS_RESOURCE: {
size_t len;
if (php_stream_copy_to_stream_ex(srcstream, stream, PHP_STREAM_COPY_ALL, &len) != SUCCESS) {
numbytes = -1;
} else {
numbytes = len;
}
break;
}
case IS_NULL:
case IS_LONG:
case IS_DOUBLE:
case IS_BOOL:
case IS_CONSTANT:
convert_to_string_ex(&data);

case IS_STRING:
if (Z_STRLEN_P(data)) {
numbytes = php_stream_write(stream, Z_STRVAL_P(data), Z_STRLEN_P(data));
if (numbytes != Z_STRLEN_P(data)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, “Only %d of %d bytes written, possibly out of free disk space”, numbytes, Z_STRLEN_P(data));
numbytes = -1;
}
}
break;

case IS_ARRAY:
if (zend_hash_num_elements(Z_ARRVAL_P(data))) {
int bytes_written;
zval **tmp;
HashPosition pos;

zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(data), &pos);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(data), (void **) &tmp, &pos) == SUCCESS) {
if (Z_TYPE_PP(tmp) != IS_STRING) {
SEPARATE_ZVAL(tmp);
convert_to_string(*tmp);
}
if (Z_STRLEN_PP(tmp)) {
numbytes += Z_STRLEN_PP(tmp);
bytes_written = php_stream_write(stream, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
if (bytes_written < 0 || bytes_written != Z_STRLEN_PP(tmp)) {
if (bytes_written < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, “Failed to write %d bytes to %s”, Z_STRLEN_PP(tmp), filename);

} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, “Only %d of %d bytes written, possibly out of free disk space”, bytes_written, Z_STRLEN_PP(tmp));
}
numbytes = -1;
break;
}
}
zend_hash_move_forward_ex(Z_ARRVAL_P(data), &pos);
}
}
break;

case IS_OBJECT:
if (Z_OBJ_HT_P(data) != NULL) {
zval out;
//看看对像怎么保存的:)
if (zend_std_cast_object_tostring(data, &out, IS_STRING TSRMLS_CC) == SUCCESS) {
numbytes = php_stream_write(stream, Z_STRVAL(out), Z_STRLEN(out));
if (numbytes != Z_STRLEN(out)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, “Only %d of %d bytes written, possibly out of free disk space”, numbytes, Z_STRLEN(out));
numbytes = -1;
}
zval_dtor(&out);
break;
}
}
default:
numbytes = -1;
break;
}
php_stream_close(stream);

if (numbytes < 0) {
RETURN_FALSE;
}

RETURN_LONG(numbytes);
}
-->


什么时候用fwrite,file_put_contents ?

1,函数原型已经说明了它们处理的数据类型不一样
2,简单的文件处理,追求速度用fwrite
3,书写简单用file_put_contents – (啥类型的数据都能处理,magic阿。但是要理解类型判断机制,否则保存的数据可能不是你想要的)

xdebug可以调试程序出错时的错误代码行数及错误代码细节了,我们下面来看一篇phpstorm+xdebug远程调试服务器PHP代码的教程,具体的如下。

phpstorm+xdebug如何调试本地代码,代码应该都熟悉了。本文说的是如何调试线上服务器的代码。我的PHP环境是lnmp1.2。

安装xdebug

在服务器上执行

pecl install xdebug
就会安装好xdebug.so模块。

编辑配置

编辑 /usr/local/php/etc/php.ini 在末尾加上以下代码:

[Xdebug]
zend_extension="xdebug.so"
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=你本地的公网IP
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.idekey="PHPSTORM"

更改完毕后在终端执行

lnmp php-fpm reload

服务器的配置到此为止。接下来看IDE的配置。

配置Server

打开Settings=>Languages & Frameworks=>PHP=>Servers

红框中的信息都是线上服务器的信息。

配置DBGp Proxy

打开Settings=>Languages & Frameworks=>PHP=>Debug=>DBGp Proxy

其中红框处需要和服务器的xdebug配置文件一致。

路由端口映射

由于大部分朋友公司都有路由器的,所以本机IP是局域网IP,这里需要将路由器的端口映射到本地。具体规则如下:

服务器Xdebug端口=>本地端口,笔者这里把路由器的9000端口映射到本地的9000端口。

开始调试

打开项目的运行配置,选择“PHP Web Application”

笔者服务器这里是https的,所以加了https,各位读者请根据实际情况填写网址。

点击IDE的“电话”图标开启远程监听。

打开代码,在如图位置断点

点击“瓢虫”图标,

IDE下方已经可以检测到调试了。

PHP发送POST请求我们使用的是curl来操作了,下面来看看一些常用的post数据的例子,具体的一起和111cn小编来看看吧。

在PHP开发的过程中经常需要发送POST请求,POST相比GET要安全很多,而且传输的数据量也较大。下面PHP程序员雷雪松就带大家一起总结下PHP发送POST请求的几种常用方式,分别使用curl、file_get_content来实现POST请求和传递参数。

1、curl实现PHP POST请求和传递参数。


$data=array("username"=>"raykaeso","name"=>"雷雪松");//post参数
$url="http://www.111cn.net";
$ch = curl_init();//创建连接
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));//将数组转换为URL请求字符串,否则有些时候可能服务端接收不到参数
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); //接收服务端范围的html代码而不是直接浏览器输出
curl_setopt($ch, CURLOPT_HEADER, false);
$responds = curl_exec($ch);//接受响应
curl_close($ch);//关闭连接

2、file_get_content实现PHP POST请求和传递参数


$data=array("username"=>"raykaeso","name"=>"雷雪松");//post参数
$url="http://www.111cn.net";
$content = http_build_query($data);
$length = strlen($content);
$options = array(
'http' => array(
'method' => 'POST',
'header' =>
"Content-type: application/x-www-form-urlencoded\r\n" .
"Content-length: $length \r\n",
'content' => $content
)
);
file_get_contents($url, false, stream_context_create($options));

本文章来为各位介绍一篇关于PHP防止XSS攻击之过滤、验证和转义之转义的例子,希望这篇教程能够帮助到各位朋友。

PHP 转义实现

把输出渲染成网页或API响应时,一定要转义输出,这也是一种防护措施,能避免渲染恶意代码,造成XSS攻击,还能防止应用的用户无意中执行恶意代码。

我们可以使用前面提到的htmlentities函数转移输出,该函数的第二个参数一定要使用ENT_QUOTES,让这个函数转义单引号和双引号,而且,还要在第三个参数中指定合适的字符编码(通常是UTF-8),下面的例子演示了如何在渲染前转义HTML输出:

<?php
$output = '<p><script>alert(“欢迎来到Laravel学院!")</script></p>';
echo htmlentities($output, ENT_QUOTES, ‘UTF-8');
如果不转义直接输出,会弹出提示框:

alert

转义之后输出变成:

<p><script>alert("欢迎访问Laravel学院!");</script></p>
现代PHP支持许多模板引擎,这些模板引擎在底层已经为了做好了转义处理,比如现在流行的twig/twig和smarty/smarty都会自动转义输出。这种默认处理方式很赞,为PHP Web应用提供了有力的安全保障。

Blade 模板引擎避免XSS攻击原理
Laravel使用的模板引擎是Blade,关于Blade的使用可以参考其官方文档,这里我们简单探讨下Laravel底层如何对输出进行转义处理。

一般我们在Laravel中返回视图内容会这么做:

return view(’test’, [‘data’=>$data]);
这是一个很简单的例子,意味着我们会在resources/views目录下找到test.blade.php视图文件,然后将$data变量传入其中,并将最终渲染结果作为响应的内容返回给用户。那么这一过程经历了哪些底层源码的处理,如果$data变量中包含脚本代码(如JavaScript脚本),又该怎么去处理呢?接下来我们让来一窥究竟。

首先我们从辅助函数view入手,当然这里我们也可以使用View:make,但是简单起见,我们一般用view函数,该函数定义在Illuminate\Foundation\helpers.php文件中:

function view($view = null, $data = [], $mergeData = [])
{
    $factory = app(ViewFactory::class);
    if (func_num_args() === 0) {
        return $factory;
    }

    return $factory->make($view, $data, $mergeData);
}
该函数中的逻辑是从容器中取出视图工厂接口ViewFactory对应的实例$factory(该绑定关系在Illuminate\View\ViewServiceProvider的register方法中注册,此外这里还注册了模板引擎解析器EngineResolver,包括PhpEngine和载入BladeCompiler的CompilerEngine,以及视图文件查找器FileViewFinder,一句话,这里注册了视图解析所需的所有服务),如果传入了参数,则调用$factory上的make方法:

public function make($view, $data = [], $mergeData = [])
{
    if (isset($this->aliases[$view])) {
        $view = $this->aliases[$view];
    }

    $view = $this->normalizeName($view);

    $path = $this->finder->find($view);

    $data = array_merge($mergeData, $this->parseData($data));

    $this->callCreator($view = new View($this, $this->getEngineFromPath($path), $view, $path, $data));

    return $view;
}
这个方法位于Illuminate\View\Factory,这里所做的事情是获取视图文件的完整路径,合并传入变量,$this->getEngineFromPath会通过视图文件后缀获取相应的模板引擎,比如我们使用.blade.php结尾的视图文件则获得到的是CompilerEngine(即Blade模板引擎),否则将获取到PhpEngine,然后我们根据相应参数实例化View(Illuminate\View\View)对象并返回。需要注意的是View类中重写了__toString方法:

public function __toString()
{
    return $this->render();
}
所以当我们打印$view实例的时候,实际上会调用View类的render方法,所以下一步我们理所应当研究render方法做了些什么:

public function render(callable $callback = null)
{
    try {
        $contents = $this->renderContents();
        $response = isset($callback) ? call_user_func($callback, $this, $contents) : null;

        // Once we have the contents of the view, we will flush the sections if we are
        // done rendering all views so that there is nothing left hanging over when
        // another view gets rendered in the future by the application developer.
        $this->factory->flushSectionsIfDoneRendering();

        return ! is_null($response) ? $response : $contents;
    } catch (Exception $e) {
        $this->factory->flushSections();

        throw $e;
    } catch (Throwable $e) {
        $this->factory->flushSections();
 
        throw $e;
    }
}
这里重点是$this->renderContents()方法,我们继续深入研究View类中的renderContents方法:

protected function renderContents()
{
    // We will keep track of the amount of views being rendered so we can flush
    // the section after the complete rendering operation is done. This will
    // clear out the sections for any separate views that may be rendered.
    $this->factory->incrementRender();
    $this->factory->callComposer($this);

    $contents = $this->getContents();

    // Once we've finished rendering the view, we'll decrement the render count
    // so that each sections get flushed out next time a view is created and
    // no old sections are staying around in the memory of an environment.
    $this->factory->decrementRender();

    return $contents;
}
我们重点关注$this->getContents()这里,进入getContents方法:

protected function getContents()
{
    return $this->engine->get($this->path, $this->gatherData());
}
我们在前面已经提到,这里的$this->engine对应CompilerEngine(Illuminate\View\Engines\CompilerEngine),所以我们进入CompilerEngine的get方法:

public function get($path, array $data = [])
{
    $this->lastCompiled[] = $path;
    // If this given view has expired, which means it has simply been edited since
    // it was last compiled, we will re-compile the views so we can evaluate a
    // fresh copy of the view. We'll pass the compiler the path of the view.
    if ($this->compiler->isExpired($path)) {
        $this->compiler->compile($path);
    }

    $compiled = $this->compiler->getCompiledPath($path);

    // Once we have the path to the compiled file, we will evaluate the paths with
    // typical PHP just like any other templates. We also keep a stack of views
    // which have been rendered for right exception messages to be generated.
    $results = $this->evaluatePath($compiled, $data);

    array_pop($this->lastCompiled);

    return $results;
}
同样我们在之前提到,CompilerEngine使用的compiler是BladeCompiler,所以$this->compiler也就是Blade编译器,我们先看$this->compiler->compile($path);这一行(首次运行或者编译好的视图模板已过期会进这里),进入BladeCompiler的compile方法:

public function compile($path = null)
{
    if ($path) {
        $this->setPath($path);
    }
    if (! is_null($this->cachePath)) {
        $contents = $this->compileString($this->files->get($this->getPath()));

        $this->files->put($this->getCompiledPath($this->getPath()), $contents);
    }
}
这里我们做的事情是先编译视图文件内容,然后将编译好的内容存放到视图编译路径(storage\framework\views)下对应的文件(一次编译,多次运行,以提高性能),这里我们重点关注的是$this->compileString方法,该方法中使用了token_get_all函数将视图文件代码分割成多个片段,如果片段是数组的话则循环调用$this->parseToken方法:

protected function parseToken($token)
{
    list($id, $content) = $token;
    if ($id == T_INLINE_HTML) {
        foreach ($this->compilers as $type) {
            $content = $this->{"compile{$type}"}($content);
        }
    }

    return $content;
}
来到这里,我们已经很接近真相了,针对HTML代码(含Blade指令代码),循环调用compileExtensions、compileStatements、compileComments和compileEchos方法,我们重点关注输出方法compileEchos,Blade引擎默认提供了compileRawEchos、compileEscapedEchos和compileRegularEchos三种输出方法,对应的指令分别是{!! !!}、{{{ }}}和{{ }},顾名思义,compileRawEchos对应的是原生输出:

protected function compileRawEchos($value)
{
    $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->rawTags[0], $this->rawTags[1]);
    $callback = function ($matches) {
        $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];

        return $matches[1] ? substr($matches[0], 1) : '<?php echo '.$this->compileEchoDefaults($matches[2]).'; ?>'.$whitespace;
    };

    return preg_replace_callback($pattern, $callback, $value);
}
即Blade视图中以{!! !!}包裹的变量会原生输出HTML,如果要显示图片、链接,推荐这种方式。

{{{}}}对应的CompileEscapedEchos,这个在Laravel 4.2及以前版本中用于转义,现在已经替换成了{{}},即调用compileRegularEchos方法:

protected function compileRegularEchos($value)
{
    $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->contentTags[0], $this->contentTags[1]);
    $callback = function ($matches) {
        $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];

        $wrapped = sprintf($this->echoFormat, $this->compileEchoDefaults($matches[2]));

        return $matches[1] ? substr($matches[0], 1) : '<?php echo '.$wrapped.'; ?>'.$whitespace;
    };

    return preg_replace_callback($pattern, $callback, $value);
}
其中$this->echoFormat对应e(%s),无独有偶,compileEscapedEchos中也用到这个方法:

protected function compileEscapedEchos($value)
{
    $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->escapedTags[0], $this->escapedTags[1]);
    $callback = function ($matches) {
        $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];

        return $matches[1] ? $matches[0] : '<?php echo e('.$this->compileEchoDefaults($matches[2]).'); ?>'.$whitespace;
    };

    return preg_replace_callback($pattern, $callback, $value);

}
辅助函数e()定义在Illuminate\Support\helpers.php中:

function e($value)
{
    if ($value instanceof Htmlable) {
        return $value->toHtml();
    }
    return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
}
其作用就是对输入的值进行转义。

经过这样的转义,视图中的{{ $data }}或被编译成<?php echo $data?>,最终如何将$data传入视图输出,我们再回到CompilerEngine的get方法,看这一段:

$results = $this->evaluatePath($compiled, $data);
evaluatePath中传入了编译后的视图文件路径和传入的变量$data,该方法定义如下:

protected function evaluatePath($__path, $__data)
{
   $obLevel = ob_get_level();ob_start();

    extract($__data, EXTR_SKIP);

    // We'll evaluate the contents of the view inside a try/catch block so we can
    // flush out any stray output that might get out before an error occurs or
    // an exception is thrown. This prevents any partial views from leaking.
    try {
        include $__path;
    } catch (Exception $e) {
        $this->handleViewException($e, $obLevel);
    } catch (Throwable $e) {
        $this->handleViewException(new FatalThrowableError($e), $obLevel);
    }

    return ltrim(ob_get_clean());
}
这里面调用了PHP系统函数extract将传入变量从数组中导入当前符号表(通过include $__path引入),其作用也就是将编译后视图文件中的变量悉数替换成传入的变量值(通过键名映射)。

好了,这就是Blade视图模板从渲染到输出的基本过程,可以看到我们通过{{}}来转义输出,从而达到避免XSS攻击的目的。

[!--infotagslink--]

相关文章

  • mysql_connect与mysql_pconnect的区别详解

    在mysql中我们会看到有两种常用的数据库连接模式,一种是长久连接,另一各是页面访问完之后就断了连接,下面我来分别介绍mysql_connect与mysql_pconnect的区别,有需要了解...2016-11-25
  • php 中file_get_contents超时问题的解决方法

    file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25
  • php file_get_contents 设置代理抓取页面示例

    file_get_contents函数在php中可以直接打开本地文件也可以直接抓取远程服务器文件,如果简单的采集我们可以使用file_get_contents直接来操作,如果有防采集我们可能需要...2016-11-25
  • 详解Maven profile配置管理及激活profile的几种方式

    这篇文章主要介绍了详解Maven profile配置管理及激活profile的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-26
  • php报错file_get_contents(): php_network_getaddresses问题

    本文章来为各位介绍一篇关于file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known...错误解决办法。 昨天,服务器的DN...2016-11-25
  • PHP file_get_contents设置超时处理方法

    file_get_contents的超时处理话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。今天说的这篇是讲超时的,确实在...2013-10-04
  • C#中out与ref的区别实例解析

    这篇文章主要介绍了C#中out与ref的区别实例解析,对C#初学者有不错的学习借鉴价值,需要的朋友可以参考下...2020-06-25
  • PHP中func_get_args(),func_get_arg(),func_num_args()的区别

    复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04
  • 谈谈Jquery中的children find 的区别有哪些

    精华:find方法能找子孙,children方法只能找儿子一、Jquery中children 语法.children(selector) 说明expr是表达式,可选参数,所有选择器中的表达式都可以用在这,比如按标签名"div",按类名".class",按序号":first"等等,如果表...2015-10-21
  • file_get_contents()获取https出现这个错误Unable to find the wrapper “https”

    下面我们来看一篇关于file_get_contents()获取https出现这个错误Unable to find the wrapper “https”问题的解决办法. file_get_contents()获取https出现这个错...2016-11-25
  • PS中像素大小、文档大小的区别

    在PS中像素大小、文档大小有什么区别呢,这个估计很多初学者不清楚,下面我来给大家讲解一下,希望对你有帮助。 1、像素大小 通常用于显示屏显示的图片大小的调整。菜...2016-09-14
  • uniapp和vue的区别详解

    这篇文章主要介绍了uniapp和vue的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-10-19
  • input框中的name和id的区别

    这篇文章主要介绍了input框中的name和id的区别介绍,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-11-22
  • C#中sleep和wait的区别分析

    这篇文章主要介绍了C#中sleep和wait的区别分析,有助于深入理解C#中线程的原理与使用技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • list与push的区别

    //函数list while(list($id,$username,$password,$add_date,$mdn,$mobile,$channel,$last_date,$area,$nickname) = mysql_fetch_array($rs)){ ...2016-11-25
  • 在PHP中使用X-SendFile头让文件下载更快

    一般来说, 我们可以通过直接让URL指向一个位于Document Root下面的文件, 来引导用户下载文件.但是, 这样做, 就没办法做一些统计, 权限检查, 等等的工作. 于是, 很多时候, 我们采用让PHP来做转发, 为用户提供文件下载...2014-06-07
  • file_put_contents并发性问题解决方案整理

    在使用file_put_contents时会碰到并发性问题了,对于这个问题我们有多种解决方案了,其实锁是小编比较喜欢的解决办法了,当然也有其它办法,具体如下。 解决 办法一,fil...2016-11-25
  • 使用maven的profile构建不同环境配置的方法

    这篇文章主要介绍了使用maven的profile构建不同环境配置的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-26
  • php提示Warning: file_get_contents(): couldn’t resolve

    在使用file_get_contents函数获取远程文件时提示Warning: file_get_contents(): couldn’t resolve错误了,这个我们可以看出是dns的问题,解决办法也简单。 今天在...2016-11-25
  • python+tifffile之tiff文件读写方式

    今天小编就为大家分享一篇python+tifffile之tiff文件读写方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27