How do I upload images to the Google Cloud Storage from PHP form?

71 views
Skip to first unread message

user3213028 via StackOverflow

unread,
Mar 5, 2014, 1:00:04 PM3/5/14
to google-appengin...@googlegroups.com

I'm currently utilizing a php app to upload images to the Google cloud storage platform, however, unlike on my local server, I am having tremendous trouble figuring out how to make this work.

Here is exactly what I am trying to do:

  1. Write the Path of the image to my Google cloud SQL
  2. Actually upload the image to the Google cloud storage platform
  3. write a script calling on the image, from the saved SQL path, to then post to my site

Can anyone point in the right direction?

Thanks!



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/22205432/how-do-i-upload-images-to-the-google-cloud-storage-from-php-form

Gerry Pesavento via StackOverflow

unread,
Mar 6, 2014, 11:40:38 AM3/6/14
to google-appengin...@googlegroups.com

Something like this worked for me with the form on GAE - upload photo from Form via php to google cloud storage given your folder permission are set...

// get image from Form
$gs_name = $_FILES["uploaded_files"]["tmp_name"]; 
$fileType = $_FILES["uploaded_files"]["type"]; 
$fileSize = $_FILES["uploaded_files"]["size"]; 
$fileErrorMsg = $_FILES["uploaded_files"]["error"]; 
$fileExt = pathinfo($_FILES['uploaded_files']['name'], PATHINFO_EXTENSION);

// change name if you want
$fileName = 'foo.jpg';

// put to cloud storage
$image = file_get_contents($gs_name);
$options = [ "gs" => [ "Content-Type" => "image/jpeg"]];
$ctx = stream_context_create($options);
file_put_contents("gs://<bucketname>/".$fileName, $gs_name, 0, $ctx);

// or move 
$moveResult = move_uploaded_file($gs_name, 'gs://<bucketname>/'.$fileName); 

The script to call the image to show on your site is typical mysqli or pdo method to get filename, and you can show the image with...

<img src="https://storage.googleapis.com/<bucketname>/<filename>"/>  


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/22205432/how-do-i-upload-images-to-the-google-cloud-storage-from-php-form/22230435#22230435
Reply all
Reply to author
Forward
0 new messages