简体   繁体   中英

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. I tried to follow instructions available on Google documentation, but it's not working. 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;

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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