简体   繁体   中英

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"] . 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.

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

The problem must be vfs://root/uploads/0 but why?

The PHP function move_uploaded_files moves a file from your web server's temporary storage to another location on the same server. The first argument to this function is the uploaded file from your client ( Ajax , or whatever upload mechanism).

Your issue is that you need to copy the uploaded file from its temporary location on your web server to Google Cloud Storage. This is a two step process (upload file, copy file to 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. The second, which has the security risk, is to allow the end user (client) to directly upload the file to Google Cloud Storage. A reasonable secure method is to use 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.

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