0

I have this folder-structure:

\out
    \MakeAvatar.php
\root
    \include
        \Calculator.php
    \img
        \avatar

What's MakeAvatar.php? That's a script which gets a parameter (like id) and makes a avatar based on that parameter. Now I need to pass a argument ($id) from Calculator.php to MakeAvatar.php. How can I do that?

Here is my code:

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

But it doesn't work. I mean the result is a unknown-image (unclear).

enter image description here

When I open that image by a editor, it is containing the content of MakeAvatar.php (all its codes). So it seems the problem is passing.


Note1: If I put MakeAvatar.php into root and pass that argument like this then if works:

... file_get_contents("http://example.com/MakeAvatar.php?id=$id")

But as you see MakeAatar.php is out of root and I cannot use http. So how can I pass an argument without http?

stack
  • 10,280
  • 19
  • 65
  • 117
  • If it is out of root, you can try using `file_get_contents` with it's full system directory path, like `/out/MakeAvatar.php` –  Apr 28 '16 at 03:01
  • Please stop asking variations of the same question. It's always the same answer. – Barmar Apr 28 '16 at 04:17
  • @Barmar Please stop marking my questions as duplicate. Yes they have a identical concept, But in this question I really need a workaround. – stack Apr 28 '16 at 04:20
  • You really need to learn how PHP and webservers work. – Barmar Apr 28 '16 at 04:22

2 Answers2

0

Your file_get_contents is actually reading the file contents, rather than executing it.

You'll either have to include your file from Calculator.php or expose it to the HTTP server and do a local http request (eg. like file_get_contents('http://localhost/path/to/your/MakeAvatar.php') )

I'd recommend wrapping your important logic in the MakeAvatar file into a function, then includeing it and executing the function.

Ben
  • 611
  • 4
  • 10
0

If your public Web server directory is root, then you should make an entry point, e.g. root/avatar.php. Let's call it controller. In the controller you should use a class(let's call it a model) which has a method generating avatars.

Thus, your structure might look like the following:

root/avatar.php

<?php
require_once __DIR__ . '/../out/MakeAvatar.php';
MakeAvatar::printPng($_GET);

out/MakeAvatar.php

<?php
class MakeAvatar {
  public static function printPng(array $input) {
    $id = $input['id'];
    echo '... PNG data for $id here...';
  }
}

Also, I'd keep models in a directory like classes or lib; controllers - in public/something, and views in different directory such as templates.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60