简体   繁体   English

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

[英]how to upload a image to google cloud storage with GAE using php

I am trying to upload an image to Google cloud storage using php code. 我正在尝试使用php代码将图像上传到Google云存储。 I tried to follow instructions available on Google documentation, but it's not working. 我尝试按照Google文档中提供的说明进行操作,但它无效。 Here is my code: 这是我的代码:

<?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>

There is no error while running the code...can someone help? 运行代码时没有错误......有人可以帮忙吗?

Found a solution: 找到了解决方案:

<?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;

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM