views:

78

answers:

6

I have a file that I'd like another script to access using file_get_contents

The file I'd like it to access is in the directory above it, so I'm using file_get_contents('../file.php?this=that')

However, it's returning No such file or directory and I can't understand why. The file is there.

I'm assuming it has something to do with it being a local file rather than a remote. Any ideas or workarounds?

A: 

Have you checked file permissions?

Does it work on a file in the same folder, or even on it's self?

jakenoble
File permissions are fine, and it works on a file in the same folder.
Rob
Also, I'm not sure who downvoted you. Hopefully they'll post a reason and an answer.
Rob
@Rob - yes a reason would be nice.....
jakenoble
The reason is simple. Permissions error message is completely different. Hint: it have a word "permission" in it.
Col. Shrapnel
Fair enough. I often confuse the two, because part of the message is the same "Failed to open stream". So thought the OP might have done the same.
jakenoble
The root of the problem is that the OP is trying to call a local file, and, in doing so, incorporate a $_GET variable. That would work, if he calls a Full URL (and `file_get_contents()` then gets a complete and executed script in return), but, trying to access the file directly as in his Question results in a **No such file** Error as there is no file actually named " *file.php?this=that* ".
Lucanos
The original poster does not mention his method of using ? to access the file like a URL in his first post. Only in his comments later. I added my response because there are parts of text in the file not found and permissions denied errors that are the same. Admitedly, I got those similar parts wrong, it's the Failed to Open Stream text that is the same.
jakenoble
A: 

There is no file in that location.
You have to use proper path.

First of all do a echo getcwd(); to see what directory is current now (from which your relative path is built)
Then double check file.php location in relation to this directory.
Check filename case, it could be sensitive.

May I ask a reason why do you open a php file with this function?

Well, an answer:

$your_var = 1;
include('../file.php');
Col. Shrapnel
The file IS in that location, and I've been able to access it when using an absolute URL. I use this function to open it so that I can send `GET` parameters to it and receive messages that it will output in regards to errors or success.
Rob
@Rob. Loool! So you're trying to open it like this `file.php?query=string` and then you wonder why a file with such a funny name has not been found?
Col. Shrapnel
Yes, I do, considering it works when I use it with a full URL
Rob
@Rob that's stupid PHP team attitude what fooled you. And lack of knowledge, of course. A single file_get_contents function is used to request two completely different realms. A filesystem and an HTTP service. A PHP developer ought to distinguish them well
Col. Shrapnel
Is there a way to do it using `file_get_contents` or something similar, rather than setting the variables and then including the file?
Rob
@Rob any reason you ask for such a way?
Col. Shrapnel
@Col. Shrapnel no reason other than pure laziness. I'd rather not redo a lot of the code at this point if there's a different workaround. I'll probably do it your way after a while, but I need a quicker fix for the moment.
Rob
@Rob The only thing you **really** need is a good book. file_get_contents do not execute php files.
Col. Shrapnel
I agree with @Col. Shrapnel's last. You presumably have code in this file that needs to be used else where. Consider putting this code in a library of reusable functions or even better Objects.
jakenoble
Actually, file_get_contents DOES execute PHP code, and no jakenoble, I don't need to use it anywhere else.
Rob
@Rob: `file_get_contents()` will only execute PHP code if the parameter inside the parenthesis is a URL, otherwise, this function will return the raw content of the file, if a file which can be directly accessed from the command line exists. A local PHP file can only be executed through using `include()`, `require()` or by loading the raw code and then `eval()` -ing those contents.
Lucanos
@Rob ignorance is not a thing to be proud of
Col. Shrapnel
@Rob: " _I'll probably do it your way after a while, but I need a quicker fix for the moment._ " Should be pretty easy to apply the Colonel's suggestion - just do a quick "Find and Replace" for the variable names within the included file, replacing "$_GET" with "$includedVars", then you just declare all the variables within the `$includedVars` array before including the file - `$includedVars['this'] = 'that';`
Lucanos
@Lucanos: I wish it were that simple, but there are various other parameters and conditional statements that would have to be modified to fit as well. @Col. Shrapnel: Your latest comment does not help, nor is it a contribution. I understand you know a lot about PHP, but that is no reason to be rude to users that do not. You seem to troll users a lot more than you help them. It's very immature and annoying.
Rob
@Rob it wasn't even intended to help. Because you showed already that you refuse to learn and accept any useful advises. It IS very childish, arrogant and annoying. So, trying to help you is just waste of time. And it is not me to blame.
Col. Shrapnel
@Col. Shrapnel: No, I've noticed that you do this quite often. You talk down to people that are less knowledgeable, which would only hinder them rather than help. And helping **is** the intent of this website, is it not?
Rob
@Rob it is just not truth. I am **always** trying help. Like in this very case. I've made a shitload of work to find out your **real** problem and give you a **proper** solution. And you still dare to say that I am not helping anyone? Actually I weren't expect anything else from you. Helping is the intent of this website, yes. But one who asks is not a boss and ones who answers are not your slaves. You cannot demand help. Especially if you're going wrong way and refuse to go a right one.
Col. Shrapnel
@Col. Shrapnel: **always**? So when you said "Ignorance is not a thing to be proud of," that was meant as help? I never demanded help, but I do expect that if you're going to post, you should have the maturity to not laugh at the user asking for help, and talk down to him
Rob
@Rob again. I've explained it all already: 1. I was trying to help. And it was me, who found out what is your real problem, despite of your fault - not posting the real code. 2. But you persist to stay wrong way based on ignorance (saying that `file_get_contents DOES execute PHP code` IS ignorance). 3 **Only then** I gave up. Because it's impossible to help someone who refuse to listen. The most funny part in all this is that there are many your faults and you blame anyone but yourself :)
Col. Shrapnel
@Col. Shrapnel where have I blamed anyone for my mistake in the matter? In fact if you weren't so rude about the way you told me of my mistake, I would have thanked you.
Rob
@Rob I don't need your thanks. Just stop accusing me is enough.
Col. Shrapnel
@Col. Shrapnel the only thing I'm accusing you of is being rude, which you constantly are, and looking through your other comments and answers, I'm not the only one that thinks so.
Rob
@Rob well at least you do not accusing me anymore for not being helpful. I'm mighty grateful. Sometimes I lost my temper, yes. And I admit it's not the best behavior. But there are always a reason. This site suffers from lots of not-professional approaches.
Col. Shrapnel
@Col. Shrapnel I never meant to accuse you of not being helpful, other than in some of your comments. You've helped me quite a bit on this site, all I ask is that you do so in a less rude fashion. You may get more appreciative responses from myself as well as many others if you tone down the elitism
Rob
@Rob what's the use of such "appreciative responses"? No kidding, I really don't know. The only appreciative response is to ask me "where am I wrong and how can I do better?". Others are just a lot of air.
Col. Shrapnel
@All: Calm down guys. Rob: As much as getting short and brutal answers are a thorn in the side (I have had my fair share, from various users, as well) they are part and parcel for the site. All you, or I, can do is ensure that our feedback and interactions are as encouraging as possible. "We need to be the change we wish to see in this world" -Ghandi.
Lucanos
Colonel: Your answer was, as you point out, correct, and was the first to detail the fault. Your assistance, and contribution to SO is appreciated. You obviously have a great deal of experience and skills, and people will embrace that all the more if you simply allow for the fact that, as with all pursuits, the majority of PHP developers are less than experts - we all start from somewhere, and it is the encouragement, and advice, of experts like yourself which make or break us as developers. If an answer adds to a user's technical ability, but breaks their passion to learn, we all lose.
Lucanos
@Lucanos: both of your responses were very well put. I especially like the Ghandi quote.
Rob
A: 

is getcwd() == dirname(__FILE__) ?

Once, I have encountered a problem where using a relative path was always throwing an error on some shared host. We ended up using absolute paths using dirname(__FILE__) as base path (setting a constant in the bootstrap and using that constant as base path value) and everything worked fine. We didn't dig further into the problem, but maybe you are encountering the same thing. I'm just guessing here.

Yanick Rochon
A: 

I find it easiest in PHP to prefix everything with absolute paths, so I'd do something like:

file_get_contents($_SERVER["DOCUMENT_ROOT"] . '/path/to/file.php');

(assuming you're running this via a web server)

Doug Hurst
The crux of the problem here is that he is trying to call a local file and incorporate a $_GET variable in doing so.
Lucanos
+2  A: 
file_get_contents('../file.php?this=that')

This will never work, unless you build a full URL with the entire http://.... syntax. PHP will see this as a request to get a file named file.php?this=that one level above the current directory. It won't treat it as a relative URL and do an HTTP request, it'll use the local filesystem handler instead. You may very well have file.php up there, but since the local file system has no concept of URLs or query parameters, it won't know to strip off the ?this=that and just load file.php. Hence your 'no such file error'.

Marc B
Well is there a way to work around it without including the file, then?
Rob
Sure, just use an absolute url. `file_get_contents('http://example.com/some/dirs/file.php?this=that');` which WILL go through the HTTP handler and most likely work as you want. But it seems silly to do a full round-trip through HTTPland just to get something from another PHP file. Rather like boiling spaghetti one noodle at a time and heating up fresh water for each noodle.
Marc B
Well see that's not much of an option either. I need to make it a bit more portable, and thus the url of file.php will change, but it will continue to be one directory above the file accessing it. Hold on, let me go look at a few `$_SERVER` keys and see if I can figure something out. P.S. I like your simile
Rob
No reason you have to hardcode the hostname/path, you can build it using the stuff returned from `parse_url($_SERVER['PHP_SELF'])` and adding the relative `../file.php?this=that`. As long as it LOOKS like a URL, then file_get_contents will go through the HTTP handler.
Marc B
A: 

I went ahead and used a few $_SERVER variables, combining them together to get the full URL and used it in file_get_contents:

file_get_contents('http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) . '/../file.php?this=that');

That did the trick. Thanks for the help everyone.

Rob
Two downvotes and no reasons or comments? I ask for workarounds and no one provides me any. I find one and people get upset and thus downvote? Lol
Rob