Google云端存储|PHP |上载的文件为0字节 [英] Google Cloud Storage | PHP | Uploaded files are at 0 Bytes

查看:60
本文介绍了Google云端存储|PHP |上载的文件为0字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Codeigniter应用程序,并试图为用户提供上传文件的访问权限.服务器安装在运行CentOS 7和Apache 2的Google Compute Engine VM上,我试图使用Google Cloud Storage进行用户上传.目前,当我上传文件时,只有文件名被上传到GCS Bucket中,文件大小为0字节.

I am using a Codeigniter application and trying to provide access for users to upload files. the server is setup on a Google Compute Engine VM running CentOS 7 and Apache 2, I am trying to use Google Cloud Storage for user upload. Currently when I upload files only the file names are being uploaded into GCS Bucket with the file size as 0 bytes.

我的前端代码:

<body>
    <form action="/includes/includes_gc/do_upload" class="dropzone" id="pfUploads" enctype="multipart/form-data">
    </form>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/min/dropzone.min.js"></script>
    <script>
        Dropzone.options.pfUploads = {
            paramName: "file",
            uploadMultiple: true,
            init: function () {
                this.on('complete', function (data) {
                    console.log(data);
                });
            }
        };
    </script>
</body>

控制器功能:

public function do_upload() {

    if ($_FILES) {
        $filename = $_FILES['file']['name'];
        $gs_name = file_get_contents($_FILES['file']['tmp_name']);
        $bucketName = 'xxxxxx.appspot.com';
        $kdir = dirname(getcwd(), 2);
        $storage = new StorageClient([
            'keyFile' => json_decode(file_get_contents($kdir . DIRECTORY_SEPARATOR . 'xxxxxx.json'), true),
            'projectId' => 'xxxxxx'
        ]);
        $file = fopen($gs_name, 'r');
        $bucket = $storage->bucket($bucketName);
        $bucket->upload($file, [
            'name' => $filename
        ]);

        $test = array();
        array_push($test, basename($gs_name));
        array_push($test, $bucketName);
        array_push($test, $filename);
        echo json_encode($test);
    }
}

对于解决此问题,我将不胜感激.

I would appreciate any help to get this resolved.

在此先感谢.纳文(Naveen)

Thanks in Advance. Naveen

推荐答案

您要为变量 gs_name 分配函数

You’re assigning to the variable gs_name the return value of the function file_get_contents and you're passing that variable to the fopen function.

如果没有通常与该文件内容相同的路径名,请 fopen

If there isn’t a path name which is the same as the contents of such file, which is normally expected, fopen returns the value false.

因此,您将值false作为第一个参数传递给

So you are passing the value false as a first parameter to $bucket->upload which effectively creates a blob of 0 bytes with the name you provided on the respective bucket.

因此,底线应该起作用:

So bottom line, the following should work:

    public function do_upload() {

        if ($_FILES) {
            $filename = $_FILES['file']['name'];
            $gs_name = $_FILES['file']['tmp_name'];
            $file = file_get_contents($gs_name);       
            $bucketName = 'xxxxxx.appspot.com';
            $kdir = dirname(getcwd(), 2);
            $storage = new StorageClient([
                'keyFile' => json_decode(file_get_contents($kdir . DIRECTORY_SEPARATOR . 'xxxxxx.json'), true),
                'projectId' => 'xxxxxx'
            ]);
            $bucket = $storage->bucket($bucketName);
            $bucket->upload($file, [
                'name' => $filename
            ]);
        }
    }

还请记住,您可以使用 upload 方法的返回值,例如:

Also keep in mind that you can use the return value of the upload method like:

$storage_object = $bucket->upload($file, ['name' => $filename]);

要进一步处理.

这篇关于Google云端存储|PHP |上载的文件为0字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆