百科狗-知识改变命运!

pclose() - 关闭进程文件指针 - php 文件目录函数

梵高2年前 (2023-11-21)阅读数 25#技术干货
文章标签指针

pclose()

(PHP 4, PHP 5, PHP 7)

关闭进程文件指针

说明

pclose(resource $handle): int

关闭用popen()打开的指向管道的文件指针。

pclose() - 关闭进程文件指针 - php 文件目录函数

参数

$handle

文件指针必须有效,且必须是成功调用popen()所返回的。

返回值

返回运行的进程的终止状态。发生错误时会返回-1

范例

Example #1pclose()例子


注释

Note:Unix Only:

proc_close()is internally implemented using thewaitpid(3)system call. To obtain the real exit status code thepcntl_wexitstatus()function should be used.

参见

  • popen()打开进程文件指针
Regarding the return value:
"Returns the termination status of the process that was run. In case of an error then -1 is returned. "
and the note about the exit status: "pclose() is internally implemented using the waitpid(3) system call. To obtain the real exit status code the pcntl_wexitstatus() function should be used."
The documentation of the return value is, at best, misleading. The function returns, as does proc_close():
* -1 on error,
* WEXITSTATUS(status) if WIFEXITED(status) is true, or 
* status if WIFEXITED(status) is false,
where status is the status parameter of waitpid().
This makes it impossible to differentiate between a relatively normal exit or a termination by signal, and reduces the value of the proc_close return code to a binary one (ok / something broke).
This can be seen in proc_open_rsrc_dtor() in ext/standard/proc_open.c (PHP 5.4.44, 5.6.12). 
The note advising the use of pcntl_wexitstatus is plain wrong. One cannot use pcntl_wexitstatus because it already has been used.
As I understand pclose will return 0 (on every platform) in case popen could not execute the specified command.
Since popen only returns the status wether it was able to send a command and not wether it was succesfully executed. Only the returned value of pclose can be used to check wether a command could be executed.
The termination status, as pointed out in another note, is not the same as the exit status from the process. However, something like "pclose($fp)/256" is not the correct way to extract the exit status, since this uses system- and version-specific knowledge of where in the termination status the exit status is stored. (Also, the process may not even have exited normally, so it may not have an exit status at all.)
Instead, the functions pcntl_wifexited() and pcntl_wexitstatus() should be used. They are wrappers for the C macros WIFEXITED() and WEXITSTATUS() that are designed for determining whether the process had an exit status and what that status was, respectively.
Somewhere between 4.1.1 and 4.2.3, the return value from pclose changed.
The exit status used to be in the second byte, so that the status would be (pclose($fp)/256).
It is now in the low-order byte, so the status is just pclose($fp).
Be careful.

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)
{if $zbp->Config('yd1125')->foot}