Upload files via PHP in App Inventor 2

0. Preface

Some time ago, I found an HTML game eat the little deer, so I modified the Fork project and named it eat the O bubble.

Original project warehouse:
https://github.com/arcxingye/EatKano/

Since many people asked me to customize this small game, but the efficiency is very low, because basically only need to copy and paste + change click picture, button sound effect, others can be copied.

Therefore, I am going to develop software similar to Steam's creative workshop, which can customize the above items and generate them with one click. But uploading files using FTP is not secure, so I found a way to upload files using PHP.

1, AI2 source code

source

Tip: WxBit (https://vip.wxbit.com) is recommended, drag it into the workspace to import the code!

2, PHP source code

2.1, configuration

PHP
1
2
3
4
5
6
7
8
9
<?php

define('FILE_UPLOAD_TOKEN','Blog_Tsinbei_Com');//Upload key

define('FILE_UPLOAD_PATH','./upload/');//Upload file storage directory

define('FILE_UPLOAD_MAXTIME','10');//Upload file timeout, unit: seconds

define('FILE_UPLOAD_TYPE','png,jpg,jpeg,gif,bmp,webp,json,mp3');//Upload file type, support image, JSON, audio

2.2, upload

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php

include('config.php');
isset($_POST['fileName'])?$upload_fileName = $_POST['fileName']:exit('missing parameter');
isset($_POST['fileBase64'])?$upload_fileBase64 = $_POST['fileBase64']:exit('missing parameter');
isset($_POST['fileType'])?$upload_fileType = $_POST['fileType']:exit('missing parameter');
isset($_POST['sign'])?$upload_sign = $_POST['sign']:exit('missing parameter');
isset($_POST['ts'])?$upload_ts = $_POST['ts']:exit('missing parameter');
//check timestamp
$upload_ts-time()<3&&time()-$upload_ts<FILE_UPLOAD_MAXTIME?0:exit('Request timeout');//Adjust according to the actual situation
//check signature
$upload_sign == md5($upload_fileName.$upload_fileBase64.$upload_fileType.FILE_UPLOAD_TOKEN.$upload_ts)?0:exit('Signature verification failed');
//check upload file type
$fileTypeArray = explode(',', strtolower(FILE_UPLOAD_TYPE));
in_array($upload_fileType, $fileTypeArray)?$fileType = $upload_fileType:exit('Unsupported file type');
// save the file
$fileID = uniqid('upload_');
file_exists(FILE_UPLOAD_PATH)?0:mkdir(FILE_UPLOAD_PATH);
$fileName = FILE_UPLOAD_PATH.$fileID.'.'.$fileType;
$myfile = fopen($fileName, "w") or die("Unable to open file!");
fwrite($myfile, base64_decode($upload_fileBase64));
fclose($myfile);
// get the file URL
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ?"https://" : "http://";
$url = $protocol . $_SERVER['HTTP_HOST'] . "/" . pathinfo($fileName)['dirname'] . "/" . pathinfo($fileName)['basename'];
//returned messages
$time_consuming = time()-$upload_ts;
echo "The file was uploaded successfully! Link: {$url} Time: {$time_consuming} seconds";

3. Download

Comment first then view it after your comment is approved. Join QQ Group to display all hidden texts.

This download service is technically supported by Tsinbei Netdisk!

Upload files via PHP in App Inventor 2

https://blog.tsinbei.com/en/archives/529/

Author
Hsukqi Lee
Posted on

2022-06-19

Edited on

2022-07-28

Licensed under

CC BY-NC-ND 4.0

Comments

Name
Mail
Site
None yet