stack stack - 11 months ago 55
PHP Question

Why file_get_contents() doesn't work without protocol?

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
, 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?

makes a avatar. And version1 creates that image as well but version2 just creates a unknown image.


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