title: Upload files to Google Cloud Storage Bucket using PHP
body: <p>I've searched a lot (and even found some helpful links like this one <a href="https://stackoverflow.com/questions/51665251/upload-a-file-to-google-cloud-storage-bucket-with-php-and-ajax/56238415#56238415">Upload a file to Google Cloud Storage Bucket with PHP and Ajax</a>) but I couldn't get it working.
  I need to upload a txt file to my google cloud storage bucket.</p>
  
  <p>Here's my code:</p>
  
  <p><strong>gcp_storage.php:</strong></p>
  
  <pre><code>&lt;?php
  
  if(isset($_FILES['file'])){
      $errors= array();
      // Authentication with Google Cloud Platform
      $client = new StorageClient(['keyFilePath' =&gt; 'cloudtest-2412**-a66e94ba56**.json']);
      $bucket = $client-&gt;bucket('cloudtest-2412**.appspot.com ');
  
      // Upload a file to the bucket.
      $file = pathinfo($_FILES['file']['name']);
      $prefix = uniqid('prefix_', true); //generate random string to avoid name conflicts
      $filename = $prefix . "." . $file['extension'];
  
      $bucket-&gt;upload(
        fopen($_FILES['file']['tmp_name'], 'r'),
        ['name' =&gt; $filename]
      );  
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>html and js:</strong></p>
  
  <pre><code>&lt;html&gt;
  &lt;form id="fileForm" action="" method="post" enctype="multipart/form-data"&gt;
      &lt;div class="form-row"&gt;
          &lt;br/&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="textFile"&gt;Text File: &lt;/label&gt;
              &lt;br/&gt;
              &lt;input type="file" name="file" id="textFile"&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;br&gt;
      &lt;button type="submit" class="btn btn-lg btn-primary"&gt; Upload &lt;/button&gt;
  &lt;/form&gt;
  
  &lt;/html&gt;
  
  &lt;script
    src="https://code.jquery.com/jquery-3.4.1.min.js"
    integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
    crossorigin="anonymous"&gt;&lt;/script&gt;
  
  &lt;script&gt;
  
      $(document).on('submit', '#fileForm', function (e) {
      e.preventDefault();
      var data = new FormData($('#fileForm').get(0));
      $.ajax({
          type: 'POST',
          url: 'gcp_storage.php',
          data: data,
          processData: false,
          contentType: false,
          success: function ($data) {
              console.log('Succcess');
              $('#success').attr('hidden', false);
          }
      });
  });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>*It shows on my console success but when I go to my bucket on google cloud, nothing appears...</p>
  
  <p>*I downloaded the .json file from the IAM page (the file is in the same directory as the other filse)</p>
  
  <p><strong><h2>EDIT 1:</h2></strong></p>
  
  <p>Don't know if this really helps, if I remove the <code>use Google\Cloud\Storage\StorageClient</code>, PHP throws me the error <code>Class 'StorageClient' not found in /base/data/home/apps/i</code> (when I put back the use code it just throws the 500 error, see below)</p>
  
  <p>I've been changing the code and putting breakpoints to see if I find where the 500 error is coming from. If I remove everything from my PHP file and just put <code>echo "Test"</code>, it works fine.</p>
  
  <p>After putting some breakpoints at the original code, I found out that the 500 error comes from this lines of code:</p>
  
  <pre><code>$client = new StorageClient(['keyFilePath' =&gt; 'cloudtest-2412**-a66e94ba56**.json']);
  $bucket = $client-&gt;bucket('cloudtest-2412**.appspot.com');
  </code></pre>
  
  <p>(The authentication code)</p>
  
  <p>Any suggestions?</p>
  
  <p>Thanks</p>