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

ぃ、小莉子 提交于 2019-12-18 09:43:31

问题


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!


回答1:


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>"/>  


来源:https://stackoverflow.com/questions/22205432/how-do-i-upload-images-to-the-google-cloud-storage-from-php-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!