如何将 Phalcon 文件对象数组转换为简单的数组数组?
How to convert Array of Phalcon File Objects to simple array of array?
我正在尝试将文件对象数组转换为数组数组,但我总是得到空结果。我尝试了下面的代码,但它给了我空数组
$json = json_encode($this->request->getUploadedFiles());
$array = json_decode($json, true);
var_dump($array);exit;
上面的结果var_dump:
array(2) { [0]=> array(0) { } [1]=> array(0) { } }
来自 POST 请求的文件对象数组:
array(2) {
[0]=> object(Phalcon\Http\Request\File)#695 (8) {
["_name":protected]=> string(52) "27657439_10157161194752222_6818734335050869731_n.jpg"
["_tmp":protected]=> string(24) "D:\xampp\tmp\php145A.tmp"
["_size":protected]=> int(31591)
["_type":protected]=> string(10) "image/jpeg"
["_realType":protected]=> NULL
["_error":protected]=> int(0)
["_key":protected]=> string(9) "path_8"
["_extension":protected]=> string(3) "jpg"
}
[1]=> object(Phalcon\Http\Request\File)#700 (8) {
["_name":protected]=> string(8) "asif.PNG"
["_tmp":protected]=> string(24) "D:\xampp\tmp\php145B.tmp"
["_size":protected]=> int(425449)
["_type":protected]=> string(9) "image/png"
["_realType":protected]=> NULL
["_error":protected]=> int(0)
["_key":protected]=> string(9) "path_14"
["_extension":protected]=> string(3) "PNG"
}
}
我需要如下结果数组:
array(2) {
["path_8"]=> array(2) {
[0]=> array(5) {
["error"]=> int(0)
["name"]=> string(14) "image-name.jpg"
["type"]=> string(10) "image/jpeg"
["tmp_name"]=> string(14) "/tmp/phpqImhgm"
["size"]=> int(222301)
}
[1]=> array(5) {
["error"]=> int(0)
["name"]=> string(42) "WhatsApp Image 2018-02-20 at 18.48.13.jpeg"
["type"]=> string(10) "image/jpeg"
["tmp_name"]=> string(14) "/tmp/php6HAWpJ"
["size"]=> int(84153)
}
}
["path_14"]=> array(1) {
[0]=> array(5) {
["error"]=> int(4)
["name"]=> string(0) ""
["type"]=> string(0) ""
["tmp_name"]=> string(0) ""
["size"]=> int(0)
}
}
}
更新: var_dump($json) 结果
string(7) "[{},{}]"
and var_dump($this->request->getUploadedFiles());退出;结果是
array(2) {
[0]=> object(Phalcon\Http\Request\File)#673 (8) {
["_name":protected]=> string(52) "27657439_10157161194752222_6818734335050869731_n.jpg"
["_tmp":protected]=> string(24) "D:\xampp\tmp\php33AF.tmp"
["_size":protected]=> int(31591)
["_type":protected]=> string(10) "image/jpeg"
["_realType":protected]=> NULL
["_error":protected]=> int(0)
["_key":protected]=> string(9) "path_8"
["_extension":protected]=> string(3) "jpg"
}
[1]=> object(Phalcon\Http\Request\File)#695 (8) {
["_name":protected]=> string(8) "asif.PNG"
["_tmp":protected]=> string(24) "D:\xampp\tmp\php33BF.tmp"
["_size":protected]=> int(425449)
["_type":protected]=> string(9) "image/png"
["_realType":protected]=> NULL
["_error":protected]=> int(0)
["_key":protected]=> string(9) "path_14"
["_extension":protected]=> string(3) "PNG"
}
}
最简单的方法是这样解析:
$files = [];
foreach($this->request->getUploadedFiles() as $file){
$item['name'] = $file->getName();
$item['realType'] = $file->getRealType();
$item['size'] = $file->getSize();
......................................
$files[] = $item;
}
或者您可以重载文件 class:
https://github.com/phalcon/cphalcon/blob/master/phalcon/Http/Request/File.zep
要实现 ArrayAccess,您还需要重载 Request class.
我正在尝试将文件对象数组转换为数组数组,但我总是得到空结果。我尝试了下面的代码,但它给了我空数组
$json = json_encode($this->request->getUploadedFiles());
$array = json_decode($json, true);
var_dump($array);exit;
上面的结果var_dump:
array(2) { [0]=> array(0) { } [1]=> array(0) { } }
来自 POST 请求的文件对象数组:
array(2) {
[0]=> object(Phalcon\Http\Request\File)#695 (8) {
["_name":protected]=> string(52) "27657439_10157161194752222_6818734335050869731_n.jpg"
["_tmp":protected]=> string(24) "D:\xampp\tmp\php145A.tmp"
["_size":protected]=> int(31591)
["_type":protected]=> string(10) "image/jpeg"
["_realType":protected]=> NULL
["_error":protected]=> int(0)
["_key":protected]=> string(9) "path_8"
["_extension":protected]=> string(3) "jpg"
}
[1]=> object(Phalcon\Http\Request\File)#700 (8) {
["_name":protected]=> string(8) "asif.PNG"
["_tmp":protected]=> string(24) "D:\xampp\tmp\php145B.tmp"
["_size":protected]=> int(425449)
["_type":protected]=> string(9) "image/png"
["_realType":protected]=> NULL
["_error":protected]=> int(0)
["_key":protected]=> string(9) "path_14"
["_extension":protected]=> string(3) "PNG"
}
}
我需要如下结果数组:
array(2) {
["path_8"]=> array(2) {
[0]=> array(5) {
["error"]=> int(0)
["name"]=> string(14) "image-name.jpg"
["type"]=> string(10) "image/jpeg"
["tmp_name"]=> string(14) "/tmp/phpqImhgm"
["size"]=> int(222301)
}
[1]=> array(5) {
["error"]=> int(0)
["name"]=> string(42) "WhatsApp Image 2018-02-20 at 18.48.13.jpeg"
["type"]=> string(10) "image/jpeg"
["tmp_name"]=> string(14) "/tmp/php6HAWpJ"
["size"]=> int(84153)
}
}
["path_14"]=> array(1) {
[0]=> array(5) {
["error"]=> int(4)
["name"]=> string(0) ""
["type"]=> string(0) ""
["tmp_name"]=> string(0) ""
["size"]=> int(0)
}
}
}
更新: var_dump($json) 结果
string(7) "[{},{}]"
and var_dump($this->request->getUploadedFiles());退出;结果是
array(2) {
[0]=> object(Phalcon\Http\Request\File)#673 (8) {
["_name":protected]=> string(52) "27657439_10157161194752222_6818734335050869731_n.jpg"
["_tmp":protected]=> string(24) "D:\xampp\tmp\php33AF.tmp"
["_size":protected]=> int(31591)
["_type":protected]=> string(10) "image/jpeg"
["_realType":protected]=> NULL
["_error":protected]=> int(0)
["_key":protected]=> string(9) "path_8"
["_extension":protected]=> string(3) "jpg"
}
[1]=> object(Phalcon\Http\Request\File)#695 (8) {
["_name":protected]=> string(8) "asif.PNG"
["_tmp":protected]=> string(24) "D:\xampp\tmp\php33BF.tmp"
["_size":protected]=> int(425449)
["_type":protected]=> string(9) "image/png"
["_realType":protected]=> NULL
["_error":protected]=> int(0)
["_key":protected]=> string(9) "path_14"
["_extension":protected]=> string(3) "PNG"
}
}
最简单的方法是这样解析:
$files = [];
foreach($this->request->getUploadedFiles() as $file){
$item['name'] = $file->getName();
$item['realType'] = $file->getRealType();
$item['size'] = $file->getSize();
......................................
$files[] = $item;
}
或者您可以重载文件 class: https://github.com/phalcon/cphalcon/blob/master/phalcon/Http/Request/File.zep 要实现 ArrayAccess,您还需要重载 Request class.