Jump to content

The symlink( ) Function


iwato

Recommended Posts

QUESTION: I have been experimenting with the symlink( ) function and have witnessed a somewhat bizarre phenomenon that I do not know how to explain. Although I am able to create s symbolic link, when the target file is opened, the PHP code within it does not appear to run. How might one explain this?Roddy

Link to comment
Share on other sites

Does the server just return the PHP code? Is the linked file outside of the web root?
In answer to your probably more important second question, the two files are only separated by the folders in which they are contained, and the folder that contains these folders is well within the root folder.Let File A be the file in which the symbol resides, and let File B be the target file to which the symbolic link points. Both files are PHP files.TEST PROCEDURE:1) When I call File A I receive the following message: Warning: symlink() [function.symlink]: File exists in blah,blah,blah on line 37. Disregarding the warning everything performs as it should, and blah,blah,blah points correctly to File A2) I click on the symbol in File A. This in turn opens File B, as one might expect. However, what is displayed in File B when it is opened by the symbolic link, and what is displayed by File B when it is opened unto itself without the link result in two different outputs.a) When File B is opened unto itself, both the HTML and the file's PHP generated output appear.:) When File B is opened by File A via the symbolic link, however, only the HTML of File B appears. No error message is produced. In fact, if I did not know that there were PHP script in File B, I would think that everything worked just fine.Roddy
Link to comment
Share on other sites

You probably can't use a symbol name for a file which already exists.
Firstly, please see the PHP website entry for the symlink function. My understanding of a target parameter is the pre-existence of a targeted file.
It sounds like it's failing to create the link in the first place, that's what that warning sounds like.
If this were the case, then it is unlikely that the file would open at all, is it not? Clearly it opens.Roddy
Link to comment
Share on other sites

QUESTION: I have been experimenting with the symlink( ) function and have witnessed a somewhat bizarre phenomenon that I do not know how to explain. Although I am able to create s symbolic link, when the target file is opened, the PHP code within it does not appear to run. How might one explain this?
In order to encourage further interest in this seemingly erudite topic please see the very practical application proposed by Olszewski Marek's entry on the PHP website.Roddy
Link to comment
Share on other sites

About this:

1) When I call File A I receive the following message: Warning: symlink() [function.symlink]: File exists in blah,blah,blah on line 37. Disregarding the warning everything performs as it should, and blah,blah,blah points correctly to File A
I don't think it's wise to disregard the warning, that might be a symptom of the problem. It would be better to try and figure out why you are getting a warning when you create the link.
My understanding of a target parameter is the pre-existence of a targeted file.
How do you know the error refers to the target parameter?
If this were the case, then it is unlikely that the file would open at all, is it not?
Frankly, I can't tell what the specifics are, we've talked about "File A", "File B", and "blah,blah,blah". Maybe you're using a link name for a file which already exists (in which case you would be opening the actual file instead of the link), I can't tell from the information given. Your description of the problem has been pretty general, but it might be something specific with your own code or environment.
Link to comment
Share on other sites

I don't think it's wise to disregard the warning, that might be a symptom of the problem. It would be better to try and figure out why you are getting a warning when you create the link.
My comment with regard to disregarding the warning was simply to say that the PHP code in the target file ( File B ) is not preventing the file from opening, as is often the case when PHP code fails. Simply, the output differs depending on whether the target file is opened unto itself or accessed via the symbolic link.
How do you know the error refers to the target parameter?
Because the directory path "blah, blah, blah" in the warning message points directly to it, and because a readlink() function contained in File A reveals the correct relative path to the symbolic link.
Maybe you're using a link name for a file which already exists (in which case you would be opening the actual file instead of the link).
The name of the targeted file is fwrite.php, and it is contained in a different folder from fwrite which is the name of the file which is the symbolic link.Perhaps I should start over: There are three files in play: there is File A that creates the symbolic link , there is File C created by the symlink( ) function, and there is File B -- the target file. Please find below the code that produces the symbolic link ( File C ) and the hyperlink that I click on to produce the unexpected result. This code includes the entire PHP script contained in File A.
		<?php			$target = '../fwrite/fwrite.php';			$link = 'fwrite';			symlink($target, $link);			if (is_link($link)) {				echo "The link corresponding to the symlink " . $link . " is: " . readlink($link);			}			else {				echo $link . " is not a symbolic link";			}		?>		<a href="fwrite">symbol</a>

Please find below the code that does not produce the desired result when opened via the hyperlink pointing to the aforementioned File C. This code includes the entire PHP script contained in File B.

						<?php			$filename = 'practice.txt';			$someContent = 'Add this text to the file.';			if (is_writable($filename)) {				$referenceVariable = fopen($filename, 'a');				if (!$referenceVariable) {					echo 'Cannot open ' . $filename;					exit;				}				if (fwrite($referenceVariable, $someContent) === FALSE) {					echo 'Cannot write to file ($filename)';					exit;				}				echo 'Success, wrote to ' . $filename . ' the following text: "' . $someContent . '"';				fclose($referenceVariable);			}			else {				echo $filename . ' is not writable';			}		?>

Roddy

Link to comment
Share on other sites

Because the directory path "blah, blah, blah" in the warning message points directly to it, and because a readlink() function contained in File A reveals the correct relative path to the symbolic link.
But the error isn't complaining about the target, it's complaining about the link name. Once you create a symlink it doesn't exist only for the duration of the script, it exists until it gets removed. When you try to create a symlink with a name that already exists, because you've already created it, you're going to get that warning.The problem in this case is that your link name does not end in .php. Even though it links to a PHP file, the symlink doesn't trigger Apache to send the file to PHP for execution. If you name the link with a PHP extension it will execute the code.
Link to comment
Share on other sites

But the error isn't complaining about the target, it's complaining about the link name. Once you create a symlink it doesn't exist only for the duration of the script, it exists until it gets removed. When you try to create a symlink with a name that already exists, because you've already created it, you're going to get that warning.
You appear to be correct on this point, because the warning message disappears when I delete File C and run File A again. We have made important progress. Unfortunately, the problem is not completely resolved. Please consider the following.As you suggested, I changed the name of the symlink( ) function's link parameter by adding the .php file extension to it. I then changed the hyperlink to the same name and clicked on it after File A was run. This is what occurred:File B produced a result exactly opposite to what is produced when the file is run independent of the symbolic link. Independent Run: Success, wrote to practice.txt the following text: "Add this text to the file."Run with Symbolic Link: practice.txt is not writable.Roddy
Link to comment
Share on other sites

The linked file may be running under a different user or with different permissions, I'm not really sure how symlinks work. The working directory might also be something other than what you expect, so you might be telling it to open a different file than what you think. It would be best to print out that type of information, you can have PHP print the current working directory to make sure it's the actual directory that the actual file is in, and PHP can also print information about what user it is running as. Have the linked file print that stuff out and make sure it's what you expect.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...