繁体   English   中英

如何使用PHP调用UploadHandler.php - blueimp jQuery文件上传

[英]How to call UploadHandler.php with PHP - blueimp jQuery File Upload

有谁知道如何使用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
));

响应包含在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;
}

我找不到通过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";

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

你可以使用基本的插件

<!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> 

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

public function post($print_response = true)

实际上从generate_response返回数据结构(这是一个包含所有已处理图像元数据的数组,如图像大小,清理网址等),但是对$ 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

希望这可以帮助!

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

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();

希望这可以帮助 !

暂无
暂无

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

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