Google云存储| PHP |上传的文件为0字节

时间:2019-04-01 05:41:36

标签: codeigniter google-app-engine upload google-cloud-storage

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

我的前端代码:

<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);
    }
}

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

预先感谢。 Naveen

2 个答案:

答案 0 :(得分:1)

您正在将变量file_get_contents的返回值分配给变量gs_name,并将该变量传递给fopen函数。

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

因此,您将值false作为第一个参数传递给$bucket->upload,这实际上会创建一个0字节的blob,其名称与您在相应存储区中提供的名称相同。

因此,底线应该起作用:

    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方法的返回值,例如:

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

进一步processing

答案 1 :(得分:0)

@fhenriques ...再次感谢您的帮助和指导。我确实玩了一些,并做了一些更改。

    if (!empty($_FILES)) {
        foreach ($_FILES['file']['tmp_name'] as $key => $tmp_name) {
            $filename = $_FILES['file']['name'][$key];
            $gs_name = $_FILES['file']['tmp_name'][$key];
            $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
            ]);
            $storage_object = $bucket->upload($file, ['name' => $filename]);
            return $storage_object;
        }
    }

现在,这有助于我使上传工作正常进行。非常感谢您的指导。我感谢您的帮助。