opcache_compile_file()
(PHP 5 >= 5.5.5, PHP 7, PECL ZendOpcache > 7.0.2)
无需运行,即可编译并缓存 PHP 脚本
说明
opcache_compile_file(string $file):boolean
该函数可以用于在不用运行某个 PHP 脚本的情况下,编译该 PHP 脚本并将其添加到字节码缓存中去。该函数可用于在 Web 服务器重启之后初始化缓存,以供后续请求调用。
参数
- $file
被编译的 PHP 脚本的路径。
返回值
如果$file被成功编译,则返回TRUE
或者在失败时返回FALSE
。
错误/异常
如果文件($file)不能被载入或者不能被编译,则会生成一个E_WARNING
级别的错误。可以使用@来抑制该警告。
参见
opcache_invalidate()
废除脚本缓存
Be aware that opcache will only compile and cache files older than the script execution start. For instance, if you use a script to generate cache files (e.g. you don't have access to shmop and rely on opcache for in-memory data caching instead), opcache_compile_file will not include the generated file in the cache, because its modification time is after the script start. The workaround is to use touch() to set a date before the script execution date, then opcache will compile and cache the generated file.
<?php /** * Get files that specified suffix * @param $path * @param array $files * @return array */ // http://www.apieye.com/528.html function getfiles( $path , &$files = array() ) { if ( !is_dir( $path ) ) return null; $handle = opendir( $path ); while ( false !== ( $file = readdir( $handle ) ) ) { if ( $file != '.' && $file != '..' ) { $path2 = $path . '/' . $file; if ( is_dir( $path2 ) ) { getfiles( $path2 , $files ); } else { if ( preg_match( "/\.(php|php5)$/i" , $file ) ) { $files[] = $path2; } } } } return $files; } $files = getfiles('/png/www/example.com/public_html/app/wordpress'); $br = (php_sapi_name() == "cli") ? "\n" : "<br />"; foreach($files as $file){ opcache_compile_file($file); echo $file.$br; }