如何使用PHP将图像上传到谷歌云存储与GAE

时间:2017-09-07 05:24:12

标签: php google-app-engine google-cloud-storage

我正在尝试使用php代码将图片上传到Google云存储。 我尝试按照Google文档中提供的说明进行操作,但它无效。 这是我的代码:

<?php
use google\appengine\api\cloud_storage\CloudStorageTools;

$options = ['gs_bucket_name' => 'myappname-178614.appspot.com'];
$upload_url = CloudStorageTools::createUploadUrl('/upload/handler', $options);
$file_name = $_FILES['uploaded_files']['name'];
$temp_name = $_FILES['uploaded_files']['tmp_name'];
move_uploaded_file($temp_name, "gs://myappname-178614.appspot.com/$file_name.jpg");
?>
<form action="<?php echo $upload_url;?>" enctype="multipart/form-data" method="post">
    Files to upload: <br>
   <input type="file" name="uploaded_files" size="40">
   <input type="submit" value="Send">
</form>

运行代码时没有错误......有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

找到解决方案:

<?php

use google\appengine\api\cloud_storage\CloudStorageTools;

$bucket = 'myapp-178614.appspot.com';
$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="test.php" 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;

?>