2015-10-07 27 views
1

美好的一天,我在我的web應用程序中使用了this upload widget如何使用ExtJS中的額外參數通過Ivan Novakov上傳控件

我使用的是FormDataUploader,我可以很好地將文件上傳到服務器目錄。但是,我想發送額外的參數以及處理上傳的php文件。這是我嘗試:

var uploadPanel = Ext.create('Ext.ux.upload.Panel', { 
    uploader : 'Ext.ux.upload.uploader.FormDataUploader', 

    uploaderOptions : { 
     url : 'uploadGallery.php' 
    }, 

    synchronous : true, 

    uploadParams : { 
     ID_Person : ID_Person, 
     ID_CI : ID_CI 
    } 

}); 

正如你所看到的,我用了uploadParams,但是,我的PHP似乎無法接受它。在我的php文件中,我有:

$ID_Person = $_GET['ID_Person']; 
$ID_CI = $_GET['ID_CI']; 

但是,我的PHP似乎無法獲得這些參數。

下一個我所做的是使用默認的ExtJS的上傳這樣:

var uploadPanel = Ext.create('Ext.ux.upload.Panel', { 
    uploaderOptions : { 
     url : 'uploadExtJS.php' 

    }, 

    synchronous : true, 

    uploadParams : { 
     ID_Person : ID_Person, 
     ID_CI : ID_CI 
    } 

}); 

起初,我用這能得到我發送的額外參數舊的PHP文件。但是,似乎我需要爲ExtJS上傳器使用不同的PHP文件。

這是我的PHP文件看起來像:

<?php 
/** 
* Example processing of raw PUT/POST uploaded files. 
* File metadata may be sent through appropriate HTTP headers: 
* - file name - the 'X-File-Name' proprietary header 
* - file size - the standard 'Content-Length' header or the 'X-File-Size' proprietary header 
* - file type - the standard 'Content-Type' header or the 'X-File-Type' proprietary header 
* 
* Raw data are read from the standard input. 
* The response should be a JSON encoded string with these items: 
* - success (boolean) - if the upload has been successful 
* - message (string) - optional message, useful in case of error 
*/ 
require __DIR__ . '_common.php'; 
$config = require __DIR__ . '_config.php'; 

error_reporting(-1); 
ini_set('display_errors', 'On'); 

/* 
* You should check these values for XSS or SQL injection. 
*/ 
if (!isset($_SERVER['HTTP_X_FILE_NAME'])) { 
    _error('Unknown file name'); 
} 
$fileName = $_SERVER['HTTP_X_FILE_NAME']; 
if (isset($_SERVER['HTTP_X_FILENAME_ENCODER']) && 'base64' == $_SERVER['HTTP_X_FILENAME_ENCODER']) { 
    $fileName = base64_decode($fileName); 
} 
$fileName = htmlspecialchars($fileName); 

$mimeType = htmlspecialchars($_SERVER['HTTP_X_FILE_TYPE']); 
$size = intval($_SERVER['HTTP_X_FILE_SIZE']); 


$inputStream = fopen('php://input', 'r'); 
// $outputFilename = $config['upload_dir'] . '/' . $fileName; 
$outputFilename = 'gallery' . '/' . $fileName; 
$realSize = 0; 
$data = ''; 

if ($inputStream) { 
    if (! $config['fake']) { 
     $outputStream = fopen($outputFilename, 'w'); 
     if (! $outputStream) { 
      _error('Error creating local file'); 
     } 
    } 

    while (! feof($inputStream)) { 
     $bytesWritten = 0; 
     $data = fread($inputStream, 1024); 

     if (! $config['fake']) { 
      $bytesWritten = fwrite($outputStream, $data); 
     } else { 
      $bytesWritten = strlen($data); 
     } 

     if (false === $bytesWritten) { 
      _error('Error writing data to file'); 
     } 
     $realSize += $bytesWritten; 
    } 

    if (! $config['fake']) { 
     fclose($outputStream); 
    } 
} else { 
    _error('Error reading input'); 
} 

if ($realSize != $size) { 
    _error('The actual size differs from the declared size in the headers'); 
} 

_log(sprintf("[raw] Uploaded %s, %s, %d byte(s)", $fileName, $mimeType, $realSize)); 
_response(); 

不過,我得到一個內部服務器的500錯誤 - 含義,有一些可能是錯了我的PHP文件。

我主要有2個問題:

  1. 如何讓uploadParams與FormDataUploader工作?
  2. 如何爲ExtJS Data Uploader編寫PHP上傳器?

任何幫助都將非常感激。

回答

0

得到它的工作。

的uploadExtJS.php文件應該是這樣:

<?php 
/** 
* Example processing of raw PUT/POST uploaded files. 
* File metadata may be sent through appropriate HTTP headers: 
* - file name - the 'X-File-Name' proprietary header 
* - file size - the standard 'Content-Length' header or the 'X-File-Size' proprietary header 
* - file type - the standard 'Content-Type' header or the 'X-File-Type' proprietary header 
* 
* Raw data are read from the standard input. 
* The response should be a JSON encoded string with these items: 
* - success (boolean) - if the upload has been successful 
* - message (string) - optional message, useful in case of error 
*/ 
// require __DIR__ . '_common.php'; 
// $config = require __DIR__ . '_config.php'; 

require_once '_common.php'; 
$config = require_once '_config.php'; 

error_reporting(-1); 
ini_set('display_errors', 'On'); 

/* 
* You should check these values for XSS or SQL injection. 
*/ 
if (!isset($_SERVER['HTTP_X_FILE_NAME'])) { 
    _error('Unknown file name'); 
} 
$fileName = $_SERVER['HTTP_X_FILE_NAME']; 
if (isset($_SERVER['HTTP_X_FILENAME_ENCODER']) && 'base64' == $_SERVER['HTTP_X_FILENAME_ENCODER']) { 
    $fileName = base64_decode($fileName); 
} 
$fileName = htmlspecialchars($fileName); 

$mimeType = htmlspecialchars($_SERVER['HTTP_X_FILE_TYPE']); 
$size = intval($_SERVER['HTTP_X_FILE_SIZE']); 


$inputStream = fopen('php://input', 'r'); 
$outputFilename = $config['upload_dir'] . '/' . $fileName; 
// $outputFilename = 'gallery' . '/' . $fileName; 
$realSize = 0; 
$data = ''; 

if ($inputStream) { 
    if (! $config['fake']) { 
     $outputStream = fopen($outputFilename, 'w'); 
     if (! $outputStream) { 
      _error('Error creating local file'); 
     } 
    } 

    while (! feof($inputStream)) { 
     $bytesWritten = 0; 
     $data = fread($inputStream, 1024); 

     if (! $config['fake']) { 
      $bytesWritten = fwrite($outputStream, $data); 
     } else { 
      $bytesWritten = strlen($data); 
     } 

     if (false === $bytesWritten) { 
      _error('Error writing data to file'); 
     } 
     $realSize += $bytesWritten; 
    } 

    if (! $config['fake']) { 
     fclose($outputStream); 
    } 
} else { 
    _error('Error reading input'); 
} 

if ($realSize != $size) { 
    _error('The actual size differs from the declared size in the headers'); 
} 

_log(sprintf("[raw] Uploaded %s, %s, %d byte(s)", $fileName, $mimeType, $realSize)); 
_response(true, "okay"); 

_common.php樣子:

<?php 

function _log($value){ 
    error_log(print_r($value, true)); 
} 


function _response($success = true, $message = 'OK'){ 
    $response = array(
     'success' => $success, 
     'message' => $message 
    ); 

    echo json_encode($response); 
    exit(); 
} 

function _error($message){ 
    return _response(false, $message); 
} 

_config.php應該是這樣的:

<?php 
return array(
    'upload_dir' => 'gallery', 
    'fake' => false 
); 
?> 

,現在我正在使用使用uniqid()microtime()的獨特名稱如使用uploadParams()屬性將圖像保存到子目錄(或主上傳/圖庫文件夾下的任何文件夾)。

編輯1:重命名上傳的文件

只是改變這一行:

$fileName = htmlspecialchars($fileName); 

到:

$fileName = uniqid() . '_' . microtime(); 

編輯3:要獲得CUSTOM子目錄從您的其他參數

首先,確保當你從你的ExtJS的Web應用程序創建上傳對話框比,你這樣做:

var uploadPanel = Ext.create('Ext.ux.upload.Panel', { 
    uploaderOptions : { 
     url : 'uploadExtJS.php' 

    }, 
    synchronous : true, 
    uploadParams : { 
     ID_1 : ID_1, 
     ID_2 : ID_2 // you can put waaay more if you want 
    } 
}); 

,並在您uploadExtJS.php,做到這一點(在那裏你定義新的文件名部分之間並在您檢查輸入流的部分)

$fileName = uniqid() . '_' . microtime(); 

$mimeType = htmlspecialchars($_SERVER['HTTP_X_FILE_TYPE']); 
$size = intval($_SERVER['HTTP_X_FILE_SIZE']); 

$ID_1 = $_GET['ID_1']; 
$ID_2 = $_GET['ID_2']; 

$newFilePath = $config['upload_dir'] . '/' . $ID_1 . '/' . $ID_2; 

if (!file_exists($newFilePath)) { 
    mkdir($newFilePath, 0777, true); 
} 

$inputStream = fopen('php://input', 'r'); 
$outputFilename = $newFilePath . '/' . $fileName; 

$realSize = 0; 
$data = ''; 

正如你所看到的,我定義的變量$newFilePath,檢查是否有人使它之前存在,然後上傳到該目錄中。

希望這可以幫助任何未來遇到問題的人。

相關問題