0

Version1: I have this code which works as well:

file_put_contents("../img/avatar/".$id.".jpg", file_get_contents("http://localhost/folder/script.php?id=$id"));

Version2: Now I need to write a path without protocol in file_get_contents, So here is the new version of my code. But it doesn't work:

$_GET['id'] = $id;
file_put_contents("../img/avatar/".$id.".jpg", file_get_contents("../folder/script.php"));

What's wrong with version2?


Note: script.php makes a avatar. And version1 creates that image as well but version2 just creates a unknown image.

stack
  • 10,280
  • 19
  • 65
  • 117
  • 1
    `file_get_contents()` doesn't interpret PHP code, it just reads files. The reason it works when tunneled through `http://` is because your http server interprets it, and gives back the result. – Havenard Apr 28 '16 at 01:09
  • http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Apr 28 '16 at 01:13
  • @Fred-ii- There is no error. Just saved photo is unknown. – stack Apr 28 '16 at 01:15
  • @Havenard Is there any workaround? – stack Apr 28 '16 at 01:15
  • Ok, Well, something I don't get though is why you have `script.php?id=$id` in version 1, but not passing it in version 2. Or is that irrelevant? – Funk Forty Niner Apr 28 '16 at 01:15
  • @Fred-ii- I don't pass it because I don't want to use `http` in the beginning of path. And I pass it by another way `$_GET['id'] = $id;` – stack Apr 28 '16 at 01:16
  • what about fread, fopen or curl? You don't need an http call for those. – Funk Forty Niner Apr 28 '16 at 01:25
  • @Fred-ii- Do you mean I can pass some arguments without writing `http` in the beginning of that path? – stack Apr 28 '16 at 01:27
  • That I couldn't say for certain about fread and fopen but curl seems to. I'd have to go through some of the docs, but it's worth a shot. Try anyone of those and see how that pans out. Might get lucky. Maybe even ajax but that may require an http call though. – Funk Forty Niner Apr 28 '16 at 01:31
  • 1
    @Fred-ii- Firstly take a look at [this](http://stackoverflow.com/questions/36849325/why-file-get-contents-cannot-open-a-filesystem-without-protocol#answer-36850587). Also [saving a image by curl](http://stackoverflow.com/questions/724391/saving-image-from-php-url#answer-724449) also needs `http`. – stack Apr 28 '16 at 01:37
  • so following on Barmar's answer/comments; how are you accessing the initial startup file (besides the call to http in `file_get_contents()`) as `http://localhost/file.php` or as a local browser file like `file:///file.php`? 2 different animals. – Funk Forty Niner Apr 28 '16 at 01:43
  • @Fred-ii- I'm not sure I get you right. But my question is about *"how can I use `file_get_contents()` without protocol"*. [My folder-structure](http://stackoverflow.com/questions/36902947/how-can-i-access-a-script-out-of-root-by-http-protocol) doesn't let me to use protocol. – stack Apr 28 '16 at 01:46

1 Answers1

2

Since your script.php is wrapped in a function, you would include the file first and then use that function as your input data.

require_once(__DIR__ . '/../../out/script.php');
file_put_contents("../img/avatar/".$id.".jpg", MakeAvatar($id));
Ali Hamze
  • 1,590
  • 13
  • 26