Jump to content

The Return Value of the fopen( ) Function


iwato

Recommended Posts

QUESTION: Does the return value of the fopen( ) function differ from platform to platform? I was expecting something entirely different from the output indicated below. Not only does the counting begin at some value other than one, but nothing is said about the content of the file.I am working in a MacOS 10.5 environment.

<?php	$file1 = fopen("../../../images/projectEmblem.png", "rb");	echo $file1 . "<br />";	$file2 = fopen("../../../forms/checkBox.html", "r");	echo $file2 . "<br />";	$file3 = fopen("../../date_function/dateFunctions.php", "r");	echo $file3;?>

When this code is run it yield the following three lines of output:Resource id #3Resource id #4Resource id #5Roddy

Link to comment
Share on other sites

That output is correct (and will always be similar regardless of platform); the first resource id probably isn't #1 because you have generated other resources beforehand in the same script (I don't know exactly how PHP decides on what ID to give a certain resource). You can read more about resources in the PHP manual: http://www.php.net/manual/en/language.types.resource.php.If you want to actually read the file's content, you need to use fread() or similar on the resource (or just use file_get_contents()).Nevertheless, there are several differences in the way fopen() handles files on different platforms, primarily relating to file-path, line-break and binary mode conventions - details can be found on its manual page.

Link to comment
Share on other sites

Thank you, Synook, for both the references and useful overview.I still have yet to understand why the Resource id always begins at 3, however. The same phenomenon occurs no matter the file or PHP script. Always, the first file opened using the fopen() function is labeled Resource id #3. Each subsequently opened file within the same PHP script is named incrementally one digit higher.Perhaps it has something to do with the way PHP is installed in my webserver. Could it be, for example, that each time PHP is initialized it creates two resources of which I am not aware?Roddy

Link to comment
Share on other sites

Perhaps it has something to do with the way PHP is installed in my webserver. Could it be, for example, that each time PHP is initialized it creates two resources of which I am not aware?
Maybe. But don't worry, it won't affect your script's performance (unless you see something like "Resource id #3423", then maybe something is wrong :)).
Link to comment
Share on other sites

AFAIK, when you run PHP in FCGI, the resource ID counter is not resetted until a recycle. So, if you're running other stripts AND are using FCGI, that could contribute to you suddenly getting 3 instead of 1.If you're not using FCGI, then I have no idea... but as said, it doesn't really matter.

Link to comment
Share on other sites

If you're not using FCGI, then I have no idea... but as said, it doesn't really matter.
Being the PHP novice that I am, I purchased MAMP Pro and am running a Dreamweaver test server with Apache as my webserver.Roddy
Link to comment
Share on other sites

I don't know as what MAMP Pro runs PHP. PHP can be ran as an Apache module, IIS6 ISAPI filter, CGI, FCGI or CLI.Being the PHP novice that you are, I suggest you don't bother with details such as this one. Accept resources as something internal to PHP that you should not look at.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...