简体   繁体   English

使用blueimp jQuery-File-Upload上传时出现权限错误

[英]Permission error on upload with blueimp jQuery-File-Upload

I have a problem with the jQuery-File-Upload. 我对jQuery-File-Upload有问题。 I'm using it with PHP code and I would to upload the file in different folder based on the logged user. 我将其与PHP代码一起使用,我将根据登录的用户将文件上传到其他文件夹中。 For example if the logged user has code 'abcd', I would to upload the file in 'upload/2017/abcd/'. 例如,如果登录的用户具有代码“ abcd”,我将在“ upload / 2017 / abcd /”中上传文件。

To do this I wrote this code: 为此,我编写了以下代码:

$(function () {
        'use strict';
        // Change this to the location of your server-side upload handler:
        var url = 'upload/<?= $year?>/<?= $logged_user?>';
        $('#fileupload').fileupload({
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            upload_dir: url,
            upload_url: url,
            url: url,
            dataType: 'json',
            done: function (e, data) {
                $.each(data.result.files, function (index, file) {
                    $('<p/>').text(file.name).appendTo('#files');
                });
            },
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $('#progress .progress-bar').css(
                    'width',
                    progress + '%'
                );
            }
        }).prop('disabled', !$.support.fileInput)
            .parent().addClass($.support.fileInput ? undefined : 'disabled');
    });

The folders exists, and has the correct permission (0777), but when I try to upload, I have this error message: 该文件夹存在,并且具有正确的权限(0777),但是当我尝试上传时,出现以下错误消息:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /upload/2017/abcd/ on this server.<br />
</p>
</body></html>

According to the docs : 根据文档

url 网址

A string containing the URL to which the request is sent. 包含请求发送到的URL的字符串。

This should be an upload handler, eg a PHP script, which will do the work of validating, naming, and moving the files into place. 这应该是一个上载处理程序,例如PHP脚本,它将执行验证,命名和将文件移动到位的工作。 The code you show above even includes a comment saying the same thing: 您上面显示的代码甚至还包含一条注释,说明同一件事:

// Change this to the location of your server-side upload handler:

But it looks like you are using url to specify a target directory to save files in: 但似乎您正在使用url指定目标目录来保存文件到:

var url = 'upload/<?= $year?>/<?= $logged_user?>';

The blueimp/jQuery-File-Upload distribution includes a set of PHP files for the server-side implementation, it looks like index.php is supposed to be the one you should point url at. blueimp / jQuery-File-Upload发行版包含一组用于服务器端实现的PHP文件 ,看来index.php应该是您应该指向url的文件。 I am not familiar with the plugin or the back end handler, but looking through the source I can see where the upload directory is specified, on line 47 : 我不熟悉插件或后端处理程序,但是通过查看源代码,我可以看到在第47行指定了上传目录的位置:

'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/',

That is one place you could do your year/user/ directory customisation. 那是您可以自定义year/user/目录的地方。

As to why you are seeing the 403 Forbidden error: as you've specified a directory as the url , that's where fileupload will POST the files. 至于为什么看到403 Forbidden错误:您已将目录指定为url ,因此fileupload将在其中发布文件。 Your url has no trailing slash ( upload/2017/abcd ), and Apache will forward that - as a GET - to the same directory with the trailing slash. 您的url没有斜杠( upload/2017/abcd ), Apache会将其作为GET 转发带有斜杠的相同目录。 Since there is no suitable index file in the directory to handle the request (no index.html , index.php , and/or whatever you have configured as DirectoryIndex in Apache), Apache will attempt to do a directory listing. 由于目录中没有合适的索引文件来处理请求(没有index.htmlindex.php和/或您在Apache中配置为DirectoryIndex任何文件),因此Apache将尝试执行目录列表。 However you have directory listing disabled ( Options -Indexes ), so you get a 403 Forbidden . 但是,您已禁用目录列表( Options -Indexes ),因此您得到403 Forbidden You should be able to verify all this by looking in your browser devtools, clicking the Network tab, and examining the requests there. 通过浏览器devtools,单击“网络”选项卡,然后查看那里的请求,您应该能够验证所有这些信息。

As a side note, you are specifying upload_dir and upload_url options for the fileupload() in your Javascript, but those are variables in the back-end PHP handler I linked to above. 附带说明一下,您在Javascript中为fileupload()指定了upload_dirupload_url选项,但这些是我上面链接到的后端PHP处理程序中的变量。 They're PHP variables, and won't have any effect in your Javascript, you should remove them to avoid confusion. 它们是PHP变量,不会对Javascript产生任何影响,因此应删除它们以避免混淆。

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

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