Setting Up Google App Engine And Uploading An Image To Google Cloud Using PHP

In this article, you’ll learn how to deploy a PHP project to Google App Engine and how to upload an image to Google Cloud Bucket. Setting up a project in GAE is easy and can be done in a few minutes. The first step is to create a new project in App Engine.

STEP 1 - Creating a new project in App Engine

  1. Go to Google Cloud console ( and log in with your Gmail id. Sign up for Google Cloud free tier services with which you will get a $300 credit with 12 months expiry.

  2. Now, on Google Cloud platform, from "products and services" tab, choose App Engine and click on dashboard.


  3. This will ask you to select/create a project. Create your project by giving a unique name. Also, please take note of your project-id which will be used while deploying the project.


  1. After creating your project, select your language and click Next.


Now, you can either complete a Google App Engine tutorial which will help you deploy a "hello world" application to your App Engine, or you can skip it and go to the next step of this article. Please note that on creating a new project in GAE , you will be assigned with default Cloud Storage Bucket with your project. To get your bucket name, go to App Engine > Settings.


Also, you can view your bucket at Products and Services > Storage > browser ..

STEP 2 - Deploying your project.

Now, you can deploy your project to the App Engine by either using shell commands in Google cloud shell or you can use Google cloud SDK to deploy your project. I will show here how to deploy using GAE SDK. You can download the SDK from this link.

  1. After installing Google Cloud SDK, open it and select "Create New Application".


  2. Enter your Project id (Created earlier) in Application name and choose runtime as PHP.


    Click "Create".
  1. Now, your project folder will contain a app.yaml and main.php file. I have edited my main.php file so as to upload a image to the Cloud Bucket. The PHP code is as follows.
    1. <?php  
    2. use google\appengine\api\cloud_storage\CloudStorageTools;  
    3. $bucket = ‘ '; // your bucket name  
    4. $root_path = 'gs://' . $bucket . '/';  
    5. $_url = '';  
    6. if(isset($_POST['submit']))  
    7. {  
    8.     if(isset($_FILES['userfile']))  
    9.     {  
    10.         $name = $_FILES['userfile']['name'];  
    11.         $file_size =$_FILES['userfile']['size'];  
    12.         $file_tmp =$_FILES['userfile']['tmp_name'];  
    13.         $original = $root_path .$name;  
    14.         move_uploaded_file($file_tmp, $original);  
    15.         $_url=CloudStorageTools::getImageServingUrl($original);  
    16.     }  
    17. }  
    19. ?>  
    20.     <html>  
    22.     <body>  
    23.         <form action="#" method="post" enctype="multipart/form-data"> Send these files:  
    24.             <p/> <input name="userfile" type="file" />  
    25.             <p/> <input type="submit" name="submit" value="Send files" /> </form>  
    26.     </body>  
    28.     </html>  
    29.     <?php  
    31. echo $_url;  
    33. ?>  

  1. Open GAE launcher SDK, select your project, and deploy.


  2. Go back to Google App Engine dashboard and open the link provided to you by App Engine. It can be found at the top-right corner of your dashboard.


  1. Upload an image.


Go to the Storage tab in the browser to see the uploaded images. 

That’s it. I hope this article will help someone. The source code is attached with project.


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now