我正在编写一个 PHP 脚本,其中涉及通过“require_once()”方法包含几个外部 PHP 脚本。我想知道是否有一种方法可以让主脚本(包括其他脚本)判断包含脚本的已处理输出是否生成了任何内容。
例如,用户对特定脚本的权限可能会导致 PHP 不生成任何输出。因此,也许主脚本会回显类似的内容:
Nothing interesting here!
有没有办法在主脚本中执行此操作,或者我需要在包含的脚本内创建这些测试,并将结果返回到主脚本?
谢谢您的宝贵时间,
斯普里诺724
I am working on a PHP script which involves me including several external PHP scripts via the "require_once()" method. I would like to know if there is a way for the master script (the one including the others) to tell whether or not the processed output from included script has generated any content.
So, for example, maybe the user's permissions for a particular script results in PHP not generating any output. So, maybe, the master script would echo something like:
Nothing interesting here!
Is there a way to do that in the master script, or would I need to create these tests inside of the included script, and return the results to the master script?
Thank you for your time,
spryno724
发布评论
评论(4)
您可以使用
ob_start
、ob_get_contents
和ob_end_clean
像这样:You can capture the output using
ob_start
,ob_get_contents
andob_end_clean
like this:如果 require_once 没有被执行,不用担心。尝试使用如下调试功能来确定 require_once 文件的变量/函数是否正常工作:-
Do not worry if the require_once is executed on not. Try to identify if the variables/functions of require_once files are working or not by using the debug function like this:-
如果您愿意,也可以使用:
当找不到文件时,include 确实返回 false,但它也会生成警告。这就是为什么你需要@。
我很想使用 file_exists() 但是正如 php 文档中的评论所述, file_exists() 不会搜索 PHP INCLUDE_PATH 并且该函数的结果被缓存。
因此,如果您决定使用 file_exists() 那么您可能会想要使用clearstatcache(),在这种情况下您可能想看看这里:
https://www.php.net/manual/en/function.clearstatcache.php#125163
和此处:
clearstatcache + include_path + 会话
If you like you can also use:
include does return false when the file can't be found, but it does also generate a warning. That's why you need the @.
I was tempted to use file_exists() however as noted in a comment in php docs, file_exists() DOESN'T SEARCH THE PHP INCLUDE_PATH and the results of this function are cached.
So if you decided to use file_exists() then you will want to probably use clearstatcache() that being the case you may want to have a look here:
https://www.php.net/manual/en/function.clearstatcache.php#125163
and here :
clearstatcache + include_path + sessions