Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉威尔+;飞行前上传到S3的Plupload响应无效-CORS_Php_Laravel_Nginx_Amazon S3_Plupload - Fatal编程技术网

Php 拉威尔+;飞行前上传到S3的Plupload响应无效-CORS

Php 拉威尔+;飞行前上传到S3的Plupload响应无效-CORS,php,laravel,nginx,amazon-s3,plupload,Php,Laravel,Nginx,Amazon S3,Plupload,我正在Nginx服务器上测试一个到S3的plupload上传。 整个项目基于Laravel 问题是,当我开始上传时,Chrome的控制台显示: XMLHttpRequest cannot load http://BUCKET.s3.amazonaws.com/. Response for preflight is invalid (redirect) 我曾尝试使用PHP头来启用CORS,但仍然出现错误 当前上载脚本: <?php $bucket = 'BUCKET'; $accessKe

我正在Nginx服务器上测试一个到S3的plupload上传。 整个项目基于Laravel

问题是,当我开始上传时,Chrome的控制台显示:

XMLHttpRequest cannot load http://BUCKET.s3.amazonaws.com/. Response for preflight is invalid (redirect)
我曾尝试使用PHP头来启用CORS,但仍然出现错误

当前上载脚本:

<?php
$bucket = 'BUCKET';
$accessKeyId = '***************';
$secret = '********************************************';

$policy = base64_encode(json_encode(array(
        'expiration' => date('Y-m-d\TH:i:s.000\Z', strtotime('+1 day')),
        'conditions' => array(
                array('bucket' => $bucket),
                array('acl' => 'public-read'),
                array('starts-with', '$key', ''),
                array('starts-with', '$Content-Type', ''),
                array('starts-with', '$name', ''),
                array('starts-with', '$Filename', ''),
        )
)));

$signature = base64_encode(hash_hmac('sha1', $policy, $secret, true));
?>
....
<div id="uploader"></div>
....    
    <script>
        $(function () {
            $("#uploader").plupload({
                runtimes: 'html5',
                url: 'http://<?php echo $bucket; ?>.s3.amazonaws.com/',

                multipart: true,
                multipart_params: {
                    'key': '${filename}',
                    'Filename': '${filename}',
                    'acl': 'public-read',
                    'Content-Type': 'image/jpeg',
                    'AWSAccessKeyId': '<?php echo $accessKeyId; ?>',
                    'policy': '<?php echo $policy; ?>',
                    'signature': '<?php echo $signature; ?>'
                },

                file_data_name: 'file',
                filters: {
                    max_file_size: '10mb',
                    prevent_duplicates: true,
                    mime_types: [
                        {title: "Image files", extensions: "jpg,jpeg"}
                    ]
                }
            });
        });
    </script>
....

非常感谢

也许值得您看看Barryvdh的cors软件包:


这解决了我的CORS问题。

我找到了一个方法来实现它。深入研究后,我没有找到一个有效的答案让Plupload工作。否则,我发现一个糟糕的解决方案是使用:

<input type="file" name="files[]" multiple>
在我的控制器中,它支持将多个文件上载到S3。但它不是异步的,而且UX更糟糕


我将继续努力,最终启用Plupload。我将在此处发布更新。

不起作用,可能与Laravel 5.1不完全兼容
<input type="file" name="files[]" multiple>
foreach($request->file("files") as $file) {
        Storage::put(
            'test'.rand(1,100),
            file_get_contents($file->getRealPath())
        );
    };