php
  • jquery
  • file-upload
  • blueimp
  • 2013-05-14 102 views 3 likes 
    3

    有誰知道如何使用PHP上傳圖片並調用UploadHandler.php?如何使用PHP調用UploadHandler.php - blueimp jQuery文件上傳

    我不確定需要傳遞哪些信息以及以什麼格式。

    這是我到目前爲止有:

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

    默認其設定' 'PARAM_NAME'=> '在類構建體files'',所以我想,預計'' –

    +0

    由於I'我會更深入地研究這一點。當它發送「文件」時,你知道它實際發送了什麼嗎?它是否等同於PHP中的file_get_contents? –

    +0

    不,它會填充你的'$ _FILES'數組和文件和信息的路徑。您可以像訪問其他數組一樣訪問它,例如:'$ _FILES ['files']'。退房http://www.php.net/manual/en/features.file-upload.post-method.php –

    回答

    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> 
    

    +0

    這使用jquery調用UploadHandler類,除非我在這裏丟失了一些東西。我希望用PHP來調用這個類。 –

    +0

    正如插件名稱所示,您需要使用jQuery上傳文件 –

    +0

    感謝Dirty-flow,但我正在尋找一種不使用jQuery的方法,並且只使用PHP調用UploadHandler類。 –

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

    我希望這會幫助你或者節省一些人的時間:)

    0

    我遇到了同樣的問題,在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 
    

    希望這有助於!

    2

    響應包含在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; 
    } 
    
    相關問題