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)
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).
PHP file (upload.php):
(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);
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: