stack stack - 7 months ago 35
PHP Question

Why file_get_contents() doesn't work without protocol?

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.

Answer

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));