GML (SOLVED) Upload file via http_request and PHP.

The-any-Key

Member
I am trying to upload a image file to a webserver via http_request. According to the helpfiles I should be able to upload a binary buffer by set it as the data. But after about 15 seconds I just get a timeout error from GM: "http request took to long".

(I tested to just send a empty string and it gives me an error from the php file so the url is right and gm can reach it.)

(I tested creating a html form and upload the file and it works fine)
Code:
<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

The file I am trying to upload is a PNG image with just one pixel in it with the image size 1x1 (70 byte in file size).

Code:
var file_to_send=argument0;
var _url=hostURL + "upload.php?";

// Open file for binary read
var binary_file=file_bin_open(file_to_send, 0);
// Get file size
var file_size=file_bin_size(binary_file);
// Create buffer to fill
var my_buffer=buffer_create(file_size,buffer_fast,1);
for (var i=0; i<file_size; i+=1)
{
    buffer_write(my_buffer,buffer_u8,file_bin_read_byte(binary_file));
}
// Rewind buffer
buffer_seek(my_buffer, buffer_seek_start, 0);

var data=my_buffer;

// Create Header
var map = ds_map_create();
ds_map_add(map, "Content-Length", string(buffer_get_size(data)));
ds_map_add(map, "User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36");
ds_map_add(map, "Content-Type", "multipart/form-data");

// Send
upload_handler = http_request(_url, "POST", map, data);
upload_handler_script = argument1;

// Clean
buffer_delete(my_buffer);
ds_map_destroy(map);
PHP file (upload.php):

Code:
<?php

header("Access-Control-Allow-Origin: *");

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($target_file))
{
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000)
{
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
{
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0)
{
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
}
else
{
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
    {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    }
    else
    {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>
 
Last edited:

zbox

Member
GMC Elder
I just get a timeout error from GM
Don't shoot the messenger! That looks like the server is just cutting off the connection because of a server side setting that cancels connections if they don't complete in X amount of seconds. Look through your server config.
 

zbox

Member
GMC Elder
Well what did you try changing on the server side ? It can be both PHP or apacha/nginx/whatever's fault ultimately they all have their own values
 

zbox

Member
GMC Elder
Also though I think I was under the impression that you were getting http errors for done reason so maybe not
 

The-any-Key

Member
http errors for done reason so maybe not
I don't think it's a response from the server. I tested to comment everything and just send a echo "test" back. But I did not get the response. I think it's just a timeout in game maker. I start to think this is a GM bug. I will try send it via a byte string instead.
 

zbox

Member
GMC Elder
Keep me updated then, I didn't realise you could send straight buffers I've been encoding my data in base64 to send it.

Humor me though, given the code above, won't your file be the POST body, not in the $_FILES["fileToUpload"]["name"] array?
 

FrostyCat

Redemption Seeker
The Manual said you can include a buffer as your request body, but it didn't say you can just dump any old data in there and expect the server to understand it. If you're going to set the MIME type to multipart/form-data, you have to follow the format. "fileToUpload" never being mentioned even once in your sending code should have been your first warning sign that the code can't possibly be right.

Since you asked for it yesterday, here is the sample you asked for from my old bug report. It's been over a year since I've last touched it, so no guarantees.
 

The-any-Key

Member
Hmm. I noticed that the data can't contain a 0 byte. If I replace all 0 bytes with 1s the upload works. This is a problem if there is a 255 byte in the file that needs to be uploaded. I think I need to send a bug report about this.
 
Last edited:

zbox

Member
GMC Elder
Hmm. I noticed that the data can't contain a 0 byte. If I replace all 0 bytes with 1s the upload works. This is a problem if there is a 255 byte in the file that needs to be uploaded. I think I need to send a bug report about this.
In string form or buffer form? Because understandable if it is a string
 

The-any-Key

Member
buffer form
Buffer.

When I send a buffer string I always offset all bytes with +1. In a string 256 just becomes a strange unicode A that the server can read. Else the string is just null point terminated with the first 0. But i wanted to spare the server from looping the buffer string and adjust back everything with -1 and save the file as binary. But it seems that is what i need to do.

Reminds me of a joke:
Two strings walk into a bar.
The bartender says: "So what'll it be?"
The first string says: "I think I'll have a beer quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy~~owmc63^Dz x.xvcu"
The second string says: "Please excuse my friend. He isn't null-terminated."
 
Last edited:

The-any-Key

Member
Ok. Solved.
I turn the file to a buffer string and offset all bytes +1.
I use the http_post_string to send the string.
In PHP. I loop the string and offset all bytes with -1 and save it to a file.
 

zbox

Member
GMC Elder
Thats a pretty yuck solution (good on you for finding it though, I mean more so in the scheme of things)
You should open a bug ticket I'd like to see this fixed
 
[QUOTE = "The-any-Key, post: 167502, membro: 12194"] Ok. Resolvido
Eu ligo o arquivo para uma string de buffer e desloco todos os bytes +1.
Eu uso o http_post_string para enviar a string.
No PHP Eu faço loop na string e desloco todos os bytes com -1 e salvei em um arquivo. [/ QUOTE]
Could you please provide the codes? As it did in your post. I'm a beginner with uploads and I'm having difficulties.
 
Top