【问题标题】:jQuery-File-Upload mysql select shows all files, not just the user's filesjQuery-File-Upload mysql select 显示所有文件,而不仅仅是用户的文件
【发布时间】:2023-04-04 18:21:01
【问题描述】:

欢迎,

我对 jQuery-File-Upload 有疑问。我将插件与 MySQL 集成 -> https://github.com/blueimp/jQuery-File-Upload/wiki/PHP-MySQL-database-integration 当我将文件添加到数据库时添加记录:name file, id_user, active... 很好添加。

加载我的网站时,加载的是列表上传的文件:https://stackoverflow.com/a/15448692/2788495

我只想加载当前加载所有的用户文件。

jQuery 代码:

    $.ajax({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: $('#fileupload').fileupload('option', 'url'),
    dataType: 'json',
    context: $('#fileupload')[0],
    formData:{
        user: '<?php echo $objSession->e_ident ?>'
    }
}).done(function (result) {
    $(this).fileupload('option', 'done')
        .call(this, null, {result: result});
});

PHP:

protected function handle_form_data($file, $index) {
    $file->title = @$_REQUEST['title'][$index];
    $file->description = @$_REQUEST['description'][$index];
    $file->user=@$_REQUEST['user'];
}
    protected function set_additional_file_properties($file) {
    parent::set_additional_file_properties($file);
    if ($_SERVER['REQUEST_METHOD'] === 'GET') {
        $sql = 'SELECT `id`,`user_id` FROM `'
            .$this->options['db_table'].'` WHERE `name`=? and user_id=?';
        $query = $this->db->prepare($sql);
        $query->bind_param('ss', $file->name, $file->user);
        $query->execute();
        $query->bind_result(
            $id,
            $user
        );
        while ($query->fetch()) {
            $file->id = $id;
            $file->user=$user;
        }
    }
}

在 JSON 中,没有选项:“id”和“user”。

【问题讨论】:

    标签: php jquery mysql jquery-file-upload


    【解决方案1】:

    我不知道我是否理解你想要做什么。

    我相信你要显示它上传的文件的用户是通过会话登录到你的系统的,是吗?

    而且这个“$objSession->e_ident”有用户ID。

    如果我是对的,你应该删除这个:

    "user: '&lt;?php echo $objSession-&gt;e_ident ?&gt;'" 来自 ajax 申请,像用户 ID 这样的东西应该只在服务器会话文件中,而不是在页面上。

    你应该把它作为用户 ID 直接放在查询绑定上,像这样: $query-&gt;bind_param('ss', $file-&gt;name, $objSession-&gt;e_ident);

    现在您的问题是,您的查询看起来正确,$objSession->e_ident 是否真的为用户提供了正确的 id?

    进入数据库 user_id 列有哪些值?

    【讨论】:

      【解决方案2】:

      这行不通。它不会从数据库中读取文件列表。数据库用于捕获有关文件的其他信息。它读取文件目录的内容。

      解决方案是启用 user_dirs。

      更改第 50 行

      'user_dirs' => false,
      

      'user_dirs' => true,
      

      我假设您已登录的用户创建了一个用户 ID 会话。第 217 行...

      protected function get_user_id() {
      @session_start();
      return session_id();
      }
      

      改成

      protected function get_user_id() {
      return $_SESSION['user_id'];
      }
      

      或者无论您的会话用户 ID 是什么。确保在 index.php 上您的会话在该页面上处于活动状态。

      这样做是创建一个与您的用户会话 user_id 同名的目录。所以他们只能看到那个目录下的文件。

      【讨论】:

        猜你喜欢
        • 2019-10-18
        • 1970-01-01
        • 2013-04-22
        • 1970-01-01
        • 2019-11-09
        • 2014-06-06
        • 2013-08-02
        • 1970-01-01
        • 2016-06-23
        相关资源
        最近更新 更多