0

I have a script into root and I'm trying to open it by file_get_contents().

This works:

file_get_contents('http://localhost/folder1/folder2/script.php?param=sth');

But this doesn't work:

file_get_contents('../folder1/folder2/script.php?param=sth');

What's wrong? How can I access a script of filesystem without writing protocol?


Note: Using __DIR__ in the beginning of that non-protocol path isn't useful. Because __DIR__ is the same something like this which isn't valid:

C:\xampp\htdocs\folder ...
stack
  • 10,280
  • 19
  • 65
  • 117
  • 2
    _But this doesn't work_ meaning what? Get params `?param=sth` are part of HTTP not the filesystem protocol. – AbraCadaver Apr 25 '16 at 19:34
  • Here you go: http://stackoverflow.com/questions/2445276/how-to-post-data-in-php-using-file-get-contents/2445332#2445332 – Red Apr 25 '16 at 19:36
  • @AbraCadaver oh ... so that's why without protocol doesn't work? And I think yes. because when I remove that parameters, this error hides *failed to open stream: ..* – stack Apr 25 '16 at 20:11

1 Answers1

3

If there's no protocol, it treats the argument as a local pathname. ? has no special meaning in Windows pathnames, so it's looking for a file whose name is script.php?param=sth, and can't find it.

Also, when accessing a local file, it simply reads the contents. Scripts are not executed, you'll just get the script source code. If you want a script to be executed, you need to go through the webserver.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    They only execute when you access them through the webserver, not when you access them as normal files. – Barmar Apr 25 '16 at 21:09
  • The webserver is configured to recognize `.php` files as scripts that need to be executed, instead of just returning the file contents. – Barmar Apr 25 '16 at 21:10
  • Simillar question without any answer http://stackoverflow.com/questions/36903392/why-file-get-contents-doesnt-work-without-protocol – stack Apr 28 '16 at 01:23
  • It's the same reason. Why did you ask it again? – Barmar Apr 28 '16 at 03:05
  • Actually my main problem is [this](http://stackoverflow.com/questions/36904467/how-can-i-pass-an-argument-to-a-script-as-get-method). – stack Apr 28 '16 at 03:06
  • Like they said in the second question, you should be using `include`, not `file_get_contents`. – Barmar Apr 28 '16 at 04:16
  • Look, I can `include()` it, but why? I need to execute it. Because when I execute it (and pass a argument to it) the output will be a image and I save that image. But if I just `include` it, there isn't any image. Please take a look at [this](http://stackoverflow.com/questions/36919485/how-can-i-execute-a-script-outside-of-root-directory-by-another-one) and please instead of marking it as duplicate, at least read the question, then you will figure out that' not duplicate. *(and if you can, write an answer)* – stack Apr 28 '16 at 16:50
  • Stop creating new questions. If you think your question should be reopened, clarify the question and request that it be reopened. – Barmar Apr 28 '16 at 16:52
  • I'm wonder, You have 200k rep and you have helped to alot of people so far. And I'm pretty sure you know the answer of my question, So why you don't tell me? All I want to know, How can I save the result of [this script](https://sourceforge.net/projects/identicons/files/identicons-1.0.1.zip/download?use_mirror=netix) as a image? – stack Apr 28 '16 at 19:28
  • I'm not even bothering to look at your questions any more. – Barmar Apr 28 '16 at 19:33
  • `:-)` seems you are angry. ok, don't take a look at my question. But why did you say "questions"? What's that "s" in the end of "question"? Ok Fine. Don't answer my current questions. But please if I ask questions in future, please take a look at them. Because my intend of asking question in here is getting a answer of you. – stack Apr 28 '16 at 19:50