Google has a nice service where you can host your applications on their servers. There is a preview version of php available for App Engine. I wanted to see how hard it would be to write a php page allowing user to upload a file to Google cloud storage.
Build with PHP on the Cloud, Google Style
Let's make a simple App that allows a user to upload a file to Google cloud storage. We are going to do this on Windows 7 with php/python google App Engine sdk.
First setup a solid Development Environment on your windows pc
-
-
Install Python 2.7.x (any version of 2.7)Download Python 2.7.x
(https://www.python.org/downloads/windows/) The local developer App Engine server needs python to communicate with Google Cloud services.
-
AppEngine PHP SDK "original sdk"
-
A Code EditorA code editor with file system with sidebar view of file system to show your app engine project folder. Try Editra for windows but there are others for example: Eclipse, sublimetext, notepad++, dreamweaver
-
Development with PHP and App Engine
-
Use App LauncherUse App Launcher installed with App Engine PHP SDK (File -> Create New Application)
-
project-id for application nameUse project-id for application name. Project id will be listed in developer's console
-
Edit app.yamlOpen your favorite code editor and edit app.yaml from your local application folder. App Engine will create default favicon.ico, app.yaml, and index.php files. Edit app.yaml file. After editing app.yaml, we will be creating a upload.php file, php.ini file. First, let's modify app.yaml. app.yaml has your application settings. In this file you give your Application a version number, Select php as runtime. Most importantly you set handlers to route links on your website to script files and static files.
In this app.yaml any link with http://your-project-id.appspot.com/inc in the application will point to inc folder. We will use inc folder for hosting css, and image static files. We could have created a url for /css , /images to organize things. You can name this anything you like and you can even have more then one static folder. A static folder can host other static folders inside without having to define each one in app.yamlapplication: props-trt-526
version: alpha003
runtime: php
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /inc
static_dir: inc
- url: /
script: main.php
- url: /upload
script: upload.php
-
Create a storage bucketCreate a storage bucket inside your project using point and click on Google cloud console. In the cloud console click "Cloud Storage" and press the button to create a storage bucket.
-
Create upload.phpFirst few lines of upload.php is the main code that sets things up. First few lines call Google cloud storage and creates a URL for your php script to upload a file from $_POST request. Topcoat css is a user interface library used to make the page look decent to use. Upload.php begins with four lines of code provided in excellent documentation by Google.When page loads the script on top of the file has retrieved a upload url from google cloud storage and it is set now as a form action for post request.
Here is the entire upload.php code viewrequire_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
$options = [ 'gs_bucket_name' => 'please-enter-bucket-name' ];
$upload_url = CloudStorageTools::createUploadUrl('/upload', $options);
<!--?php
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
$options = [ 'gs_bucket_name' =--> 'please-enter-bucket-name' ];
$upload_url = CloudStorageTools::createUploadUrl('/upload', $options);
?>
<!--?php
if(isset($_POST['do-upload']) AND $_POST['do-upload'] === "yes"){
$yesupload = $_POST['do-upload'];
preg_match("/yes/", "".$yesupload."");
$filename = $_FILES['testupload']['name'];
$gs_name = $_FILES['testupload']['tmp_name'];
move_uploaded_file($gs_name, 'gs://please-enter-bucket-name/'.$filename.'');
?-->Hey file is uploaded, Yes! ".$yesupload." ! "; echo "This is the file name: ".$filename."
"; } echo "";
?>
-
Create and Edit php.ini file for larger filesFor allowing this script to upload files you need a long enough session time(session.gc_maxlifetime 10000 sec) to let http post request send data(images, audios, videos, csv, xml and so on) to google cloud storage and you also need to set a higher upload limit(max_file_uploads 100 mb) to allow AppEngine WebServer upload to Google Cloud Storage here is how php.ini looks:
; This is a simple php.ini file on App Engine
; It enables output buffering for all requests by overriding the
; default setting of the PHP interpreter.
output_buffering = "On"
max_file_uploads = 0
upload_max_filesize = 100M
session.gc_maxlifetime = 10000
Let's Test out the upload script
Use App Engine launcher to deploy the app using your account id. App will be usable on http://your-project-id.appspot.com/upload
Download Complete Code from Github Repo
I've created a repo on Github with App Engine PHP upload app ready to go. You can download from here. Use this Git Repo If you are trying to avoid the 32MB limitation Let's connect this to CloudSQL sendReferences
Install PHP SDK on Windows (https://developers.google.com/appengine/docs/php/gettingstarted/installingwindows) Cool TopCoat UI..put a coat on your application.(http://topcoat.io/) Great Video on App Engine Launcher(https://www.youtube.com/watch?v=Ehl8O2Y6edk) User Uploads (https://developers.google.com/appengine/docs/php/googlestorage/user_upload) Beginning PHP and MySQL: From Novice to Professional, Fourth Edition