【问题标题】:JQuery File Upload Custom Path ProblemsJQuery 文件上传自定义路径问题
【发布时间】:2013-08-14 08:04:31
【问题描述】:

我正在使用 JQuery 文件上传插件来添加将文件上传到我的网站的可能性。

上传脚本位于类似“index.php?cartella_id=x”的页面中(x 是表示专辑 ID 的数字)。

我想像这样存储文件:

server/php/files
- directory 1/
- - Image x
- - Image y
- directory 2/
- - Image z
- - Image z(2)

我基本上想为每张专辑创建一个不同的目录。

按我想要的方式存储图像并不难,因为我通过使用像这样的隐藏输入来传递 $cartella_id

<input type="hidden" name="cartella_id" value="<?php echo $cartella_id; ?>">

在 server/php/index.php 文件中,我检查用户是否已登录以及相册是否像这样存在

session_start();
if(!isset($_SESSION["username"])) {
    die();
    }


if(isset($_REQUEST["cartella_id"])) {
    $cartella_id = (int) $_REQUEST["cartella_id"];
    $sql = "SELECT * FROM cartelle WHERE cartella_id = $cartella_id";
    $query = mysql_query($sql);
    if($cartella = mysql_fetch_array($query)) {
        $upload_dir = '/files/'.$cartella_id.'/';
        } else {
        die();
        }
} else {
    die();
}

在 UploadHandler.php 页面中,我编辑了“upload_dir”和“upload_url”选项。

'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/'.$_REQUEST["cartella_id"].'/',
            'upload_url' => $this->get_full_url().'/files/'.$_REQUEST["cartella_id"].'/',

上传工作正常...问题是,如果我刷新上传页面,我看不到已经上传的文件,就像脚本在没有指定自定义路径时会显示的那样。我可以使用 $_SESSION 来解决这个问题,但我不喜欢这个解决方案,而且删除按钮在任何情况下都不起作用。

我研究了很多 PHP 代码,也搜索了很多,但我找不到适合我的解决方案。

当脚本检查现有文件时如何发送自定义变量(我找不到那段代码)? 如何使删除按钮起作用?

提前致谢

【问题讨论】:

    标签: php image-uploading jquery-file-upload


    【解决方案1】:

    所以我通过编辑 js/main.js 解决了第一个问题(即使在重新加载页面后也可以看到文件)。 我编辑了这个:

    url: $('#fileupload').fileupload('option', 'url'),
    

    到这里

    url: ($('#fileupload').fileupload('option', 'url') + "?cartella_id="+ document.getElementById('cartella_id').value),
    

    仍然必须使删除按钮起作用。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,我解决了它,启用用户目录并按照此处的建议覆盖 UploadHandler 类中的方法:

      jQuery-File-Upload PHP-user-directories

      我在删除 url 中添加了一个额外的参数。

      我的 index.php :

      <?php
      
      error_reporting(E_ALL | E_STRICT);
      
      require('UploadHandler.php');
      
      class CustomUploadHandler extends UploadHandler {
      
          protected function get_user_id() {
              return $_REQUEST['recordId'];
          }
      
          protected function set_additional_file_properties($file) {
              $file->deleteUrl = $this->options['script_url']
                  .$this->get_query_separator($this->options['script_url'])
                  .$this->get_singular_param_name()
                  .'='.rawurlencode($file->name)
                  .'&recordId='.$_REQUEST['recordId']
                  ;
              $file->deleteType = $this->options['delete_type'];
              if ($file->deleteType !== 'DELETE') {
                  $file->deleteUrl .= '&_method=DELETE';
              }
              if ($this->options['access_control_allow_credentials']) {
                  $file->deleteWithCredentials = true;
              }
          }   
      
      }
      
      $upload_handler = new CustomUploadHandler(array(
          'user_dirs' => true,
      ));
      

      我改变了网址

      我的 main.js:

      $(function () {
          'use strict';
      
          // Initialize the jQuery File Upload widget:
          $('#fileupload').fileupload({
              // Uncomment the following to send cross-domain cookies:
              //xhrFields: {withCredentials: true},
              url: 'server/php/index.php?recordId=' + recordId,
          });
      
          // Enable iframe cross-domain access via redirect option:
          $('#fileupload').fileupload(
              'option',
              'redirect',
              window.location.href.replace(
                  /\/[^\/]*$/,
                  '/cors/result.html?%s'
              )
          );
      
              // Load existing files:
              $('#fileupload').addClass('fileupload-processing');
              $.ajax({
                  // Uncomment the following to send cross-domain cookies:
                  //xhrFields: {withCredentials: true},
                  url: $('#fileupload').fileupload('option', 'url'),
                  dataType: 'json',
                  context: $('#fileupload')[0]
              }).always(function () {
                  $(this).removeClass('fileupload-processing');
              }).done(function (result) {
                  $(this).fileupload('option', 'done')
                      .call(this, $.Event('done'), {result: result});
              });
      
      });
      
      }
      

      我没有编辑 UploadHandler.php

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-19
        • 1970-01-01
        • 2023-04-07
        • 2014-10-26
        • 2021-03-20
        • 1970-01-01
        • 2019-09-09
        • 2023-04-09
        相关资源
        最近更新 更多