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

时间:2013-05-14 20:46:29

标签: php jquery file-upload blueimp

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

5 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

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

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

答案 2 :(得分:2)

您可以使用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> 

答案 3 :(得分:1)

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

public function post($print_response = true)

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

希望这有帮助!

答案 4 :(得分:0)

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

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

希望这会有所帮助!