簡體   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