简体   繁体   English

使用AJAX发送的PHP中的App引擎将$ _FILES [“ file”] [“ tmp_name”]上传到Cloud Storage

[英]Upload $_FILES[“file”][“tmp_name”] to Cloud Storage with App engine in PHP sent from AJAX

I´m trying to upload a file to Cloud Storage with cloud libraries and I found that the problem is $_FILES["file"]["tmp_name"] . 我正在尝试使用cloud libraries将文件上传到Cloud Storage,但发现问题是$_FILES["file"]["tmp_name"] Usually I would use move_uploaded_file ( $_FILES["file"]["tmp_name"] , $new_file_name ) in a normal server but in appengine this is not posible you need to use this function to upload to cloud storage. 通常,我会在普通服务器中使用move_uploaded_file ( $_FILES["file"]["tmp_name"] , $new_file_name ) ,但在appengine中,这是不可能的,您需要使用此功能上传到云存储。

require __DIR__ . '/../../vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
function upload_object($bucketName, $objectName, $source) {
    $storage = new StorageClient();
    $file    = fopen($source, 'r');
    $bucket  = $storage->bucket($bucketName);
    $object  = $bucket->upload($file, [
        'name' => $objectName
    ]);
}
$new_file_name = "";
$new_file_name = "file-".rand(10,90000).".pdf";
upload_object('bucket-name', $new_file_name, $_FILES["file"]["tmp_name"]);

But this can only upload files from appengine to cloudstorage not files sent to the PHP from AJAX 但这只能将文件从appengine上传到cloudstorage而不能将文件从AJAX发送到PHP

The problem must be vfs://root/uploads/0 but why? 问题一定是vfs://root/uploads/0但是为什么呢?

The PHP function move_uploaded_files moves a file from your web server's temporary storage to another location on the same server. PHP函数move_uploaded_files将文件从Web服务器的临时存储区移到同一服务器上的另一个位置。 The first argument to this function is the uploaded file from your client ( Ajax , or whatever upload mechanism). 此函数的第一个参数是从客户端( Ajax或任何上传机制)上传的文件。

Your issue is that you need to copy the uploaded file from its temporary location on your web server to Google Cloud Storage. 您的问题是,您需要将上传的文件从Web服务器上的临时位置复制到Google Cloud Storage。 This is a two step process (upload file, copy file to GS). 这是一个两步过程(上传文件,将文件复制到GS)。

You have two choices, one has security risks. 您有两种选择,一种有安全风险。 The first is to upload the file to your web server, then copy the file to Google Cloud Storage. 首先是将文件上传到Web服务器,然后将文件复制到Google Cloud Storage。 The second, which has the security risk, is to allow the end user (client) to directly upload the file to Google Cloud Storage. 第二个方法存在安全风险,它允许最终用户(客户端)直接将文件上传到Google Cloud Storage。 A reasonable secure method is to use presigned URLs . 一种合理的安全方法是使用presigned URLs

Hopefully this answer will explain the process of client file uploads to your web server and then to Google Cloud Storage OR from your client directly to Google Cloud Storage. 希望这个答案将解释客户端文件上传到Web服务器,然后上传到Google Cloud Storage或从客户端直接上传到Google Cloud Storage的过程。

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

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