简体   繁体   English

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

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

Does anyone know how to upload images using PHP and calling the UploadHandler.php? 有谁知道如何使用PHP上传图像并调用UploadHandler.php?

I'm not sure what information needs to be passed and in what format. 我不确定需要传递哪些信息以及采用何种格式。

Here's what I have so far: 这是我到目前为止所拥有的:

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

The response is contained within the UploadHandler class object and can be retrieved like shown below. 响应包含在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;
}

I could not find a way to get the file name via php so I had to do it myself. 我找不到通过php获取文件名的方法所以我必须自己做。

First you need to add a public variable under UploadHandler.php 首先,您需要在UploadHandler.php下添加一个公共变量

class UploadHandler
{
    public $file_name;
    protected $options;

and then add that to the function that creates the name 然后将其添加到创建名称的函数中

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;
}

then under index.php you could do something like this 然后在index.php下你可以做这样的事情

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

I hope this help you or save someone some time :) 我希望这可以帮助你或节省一些时间:)

you can use 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> 

I ran into the same problem, where in the PHP I wanted to write all the URLS that UploadHandler.php had created to a mySQL database. 我遇到了同样的问题,在PHP中,我想将UploadHandler.php创建的所有URL写入mySQL数据库。 If you look through the code, you'll see that 如果你查看代码,你会看到

public function post($print_response = true)

actually returns the data structure from generate_response (which is a array with all the processed image metadata like image size, sanitized url, etc), but the call to $this->post() never does anything which it. 实际上从generate_response返回数据结构(这是一个包含所有已处理图像元数据的数组,如图像大小,清理网址等),但是对$ this-> post()的调用从不做任何事情。 So I add a variable 所以我添加一个变量

protected $upload_content = [];

to the class definition and changed the logic in function 到类定义并改变了函数中的逻辑

protected function initialize()

to

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

to update this variable after the images have been processed (you would need to do something similar with the GET case if you are using that). 在处理完图像后更新此变量(如果使用GET,则需要执行类似的操作)。 Then, I add a public function to the class to get this variable 然后,我向类中添加一个公共函数来获取此变量

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

and now the UploadHandler class can be called like this 现在可以像这样调用UploadHandler类

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

Hope this helps! 希望这可以帮助!

First of all you should create protected variable: 首先,您应该创建受保护的变量:

protected $options;
protected $uploaded_files = [];

then you should assign to this variable the response value in post() method: 那么你应该在post()方法中为这个变量赋值响应值:

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

then you should create public method which would return that response: 那么你应该创建一个返回该响应的公共方法:

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

and finally you should initiate the class and call the method: 最后你应该启动类并调用方法:

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

Hope this Helps ! 希望这可以帮助 !

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

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