Returning An Image to C# from PHP Script

In this case, the title really does say it all :slight_smile: I’m wanting to use a PHP script to grab the contents of a specified directory and return all the images. I’ve been trying to find if the result of running “imagecreatefrompng” can be returned via “echo” or if there’s a more elegant way to return a number of images from PHP to C#.

The idea is the PHP handles grabbing the data from the server and returns it to the C# which will then take what it gets and turn them into a Texture2D[ ]. If I can find a way to get a PNG image as a byte[ ] into PHP and return it, then I should be able to use “Texture2D.LoadImage” to easily convert each image to what I need.

Well this PHP doesn’t seem to quite do what I expected:

if(filter_has_var(INPUT_POST, "merchant")  filter_has_var(INPUT_POST, "test"))
{
	$merchant = filter_input(INPUT_POST, "merchant", FILTER_SANITIZE_STRING);
	$test = filter_input(INPUT_POST, "test", FILTER_SANITIZE_STRING);
}
else
{
	die ("DataMissing");
}

$data_location = $_SERVER['DOCUMENT_ROOT'] . "/" . $merchant;

$arr = array();

if (is_dir($data_location)) 
{
    $dir = opendir($data_location) or die("Couldn't access directory!");
    while (($file = readdir($dir)) !== false) 
    {
		$arr[] = file_get_contents($file);
    }
    echo $arr;
}
else
{
	// Need to create it
	mkdir($data_location, 0700) or die("Couldn't create " . $data_location);
	echo "Empty";
}

When returned to Unity, the string “Array” is the result. Come to think of it, should I even be storing it as a string on that side since technically it should be an array of byte arrays, or am I over thinking it?

The problem seems to have been with the while loop. It reads 3 files even though only two are present. In addition to the file I put there, it reads a file named “.” and one named “…” which are easily filtered out. Then I tried passing what was returned using this:

tex = new Texture2D(32, 32);
tex.LoadImage(bytes);

Which results in a white background with a red question mark instead of the image.

EDIT: Now this is interesting. Putting a “filesize” in the PHP to check the file before loading yields 4183, exactly as expected. However the return to C# via

	string result = sendData.StartSending(form, prepend + "get_images.php");
        Debug.Log(result.Length);

yields a length of 4042. It’s worth noting that I had changed the above to “echo $arr[0]” to just dump one element. Can you say “Confused”? :face_with_spiral_eyes: