【问题标题】:How to call UploadHandler.php with PHP - blueimp jQuery File Upload如何使用 PHP 调用 UploadHandler.php - blueimp jQuery 文件上传
【发布时间】:2013-05-09 06:50:50
【问题描述】:

有谁知道如何使用 PHP 上传图片并调用 UploadHandler.php?

我不确定需要传递哪些信息以及以什么格式传递。

这是我目前所拥有的:

$prop="test";
session_id($prop);
@session_start();
$url = 'http://christinewilson.ca/wp-content/uploads/2013/02/port_rhdefence.png';
$file_name[] = file_get_contents($url);

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');
$upload_handler = new UploadHandler(array(
    'user_dirs' => true
));

【问题讨论】:

  • 默认情况下它在类构造中设置'param_name' => 'files',所以我猜它期望<input type="file" name="files" />
  • 谢谢,我将进一步研究。当它发送“文件”时,你知道它实际发送的是什么吗?是否相当于 PHP 中的 file_get_contents?
  • 不,它会用文件和信息的路径填充您的$_FILES 数组。您可以像访问任何其他数组一样访问它,例如:$_FILES['files']。查看php.net/manual/en/features.file-upload.post-method.php
  • 谢谢!我会试试这个。

标签: php jquery file-upload blueimp


【解决方案1】:

响应包含在 UploadHandler 类对象中,可以如下所示进行检索。

$upload_handler = new UploadHandler();
$response = $upload_handler->response;
$files = $response['files'];
$file_count = count($files);
for ($c = 0; $c < $file_count; $c++) {
   if (isset($files[$c]->error))
       continue;
   $type = $files[$c]->type;
   $name = $files[$c]->name;
   $url = $files[$c]->url;
}

【讨论】:

    【解决方案2】:

    我找不到通过 php 获取文件名的方法,所以我必须自己做。

    首先你需要在 UploadHandler.php 下添加一个公共变量

    class UploadHandler
    {
        public $file_name;
        protected $options;
    

    然后将其添加到创建名称的函数中

    protected function get_file_name($name,
            $type = null, $index = null, $content_range = null) {
    
        $this->file_name = $this->get_unique_filename(
            $this->trim_file_name($name, $type, $index, $content_range),
            $type,
            $index,
            $content_range
        );
        return $this->file_name;
    }
    

    然后在 index.php 下你可以做这样的事情

    $upload_handler = new UploadHandler();
    echo "\r\n [" . $upload_handler->fileName . "]\r\n";
    

    我希望这可以帮助您或节省一些时间:)

    【讨论】:

      【解决方案3】:

      你可以使用the basic plugin:

      <!DOCTYPE HTML>
      <html>
      <head>
      <meta charset="utf-8">
      <title>jQuery File Upload Example</title>
      </head>
      <body>
      <input id="fileupload" type="file" name="files[]" data-url="server/php/" multiple>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      <script src="js/vendor/jquery.ui.widget.js"></script>
      <script src="js/jquery.iframe-transport.js"></script>
      <script src="js/jquery.fileupload.js"></script>
      <script>
      $(function () {
          $('#fileupload').fileupload({
              dataType: 'json',
              done: function (e, data) {
                  $.each(data.result.files, function (index, file) {
                      $('<p/>').text(file.name).appendTo(document.body);
                  });
              }
          });
      });
      </script>
      </body> 
      

      【讨论】:

      • 这使用 jquery 来调用 UploadHandler 类,除非我在这里遗漏了一些东西。我希望用 PHP 来调用这个类。
      • 正如插件名所说,上传文件需要jQuery
      • 感谢 Dirty-flow,但我正在寻找一种不使用 jQuery 并仅使用 PHP 调用 UploadHandler 类的方法。
      【解决方案4】:

      我遇到了同样的问题,我想在 PHP 中将 UploadHandler.php 创建的所有 URL 写入 mySQL 数据库。如果你看一下代码,你会看到

      public function post($print_response = true)
      

      实际上从 generate_response 中返回数据结构(这是一个包含所有已处理图像元数据的数组,如图像大小、净化的 url 等),但对 $this->post() 的调用从不执行任何操作。所以我添加了一个变量

      protected $upload_content = [];
      

      到类定义并改变函数中的逻辑

      protected function initialize()
      

              case 'POST':
                   $this->upload_content = $this->post(false);
                   break;
      

      在处理完图像后更新此变量(如果您使用 GET 案例,则需要执行类似的操作)。然后,我在类中添加一个公共函数来获取这个变量

       public function get_upload_content() {
           return $this->upload_content;
       }
      

      现在可以像这样调用 UploadHandler 类

       $upload_handler = new UploadHandler();
       $images = $upload_handler->get_upload_content();
       // Call a function that writes the urls in $images array to a database
      

      希望这会有所帮助!

      【讨论】:

        【解决方案5】:

        首先你应该创建受保护的变量:

        protected $options;
        protected $uploaded_files = [];
        

        那么你应该在 post() 方法中为这个变量分配响应值:

        $this->uploaded_files = $response;
        return $this->generate_response($response, $print_response);
        

        那么您应该创建返回该响应的公共方法:

        public function get_uploaded_files() {
                return $this->uploaded_files;
            }
        

        最后你应该启动类并调用方法:

        $uploadPicture = new UploadHandler();
                $images = $uploadPicture->get_uploaded_files();
        

        希望这会有所帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-29
          • 1970-01-01
          • 2019-01-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多