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 the Google Cloud console (https://console.cloud.google.com) 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 the Google Cloud platform, from the "products and services" tab, choose App Engine and click on Dashboard.
    PHP
  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 which will be used while deploying the project.
    Cloud plateform
  4. After creating your project, select your language and click Next.
    App
  5. 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.
    App Engine
  6. 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".
    New Application
  2. Enter your Project id (Created earlier) in the Application name and choose runtime as PHP.
    Project id
  3. Click "Create".
  4. Now, your project folder will contain an app.yaml and main.php file. I have edited my main.php file so as to upload an image to the Cloud Bucket. The PHP code is as follows.
    <?php
    use google\appengine\api\cloud_storage\CloudStorageTools;
    $bucket = ''; // your bucket name
    $root_path = 'gs://' . $bucket . '/';
    $_url = '';
    if(isset($_POST['submit']))
    {
        if(isset($_FILES['userfile']))
        {
            $name = $_FILES['userfile']['name'];
            $file_size = $_FILES['userfile']['size'];
            $file_tmp = $_FILES['userfile']['tmp_name'];
            $original = $root_path . $name;
            move_uploaded_file($file_tmp, $original);
            $_url = CloudStorageTools::getImageServingUrl($original);
        }
    }
    ?>
    
    <html>
    
    <body>
        <form action="#" method="post" enctype="multipart/form-data">
            Send these files:
            <p/> <input name="userfile" type="file" />
            <p/> <input type="submit" name="submit" value="Send files" /> </form>
    </body>
    
    </html>
    <?php
    
    echo $_url;
    
    ?>
    
  5. Open the GAE launcher SDK, select your project, and deploy.
     GAE launcher
  6. Go back to the 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.
     Google App
  7. Upload an image.
    Upload
  8. 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 to the project.


Similar Articles