我完全坚持这一点......检查了所有相关的stackoverflow帖子,没有任何帮助。在 Codeigniter 中使用 Phil 的 REST 服务器/客户端设置,可以插入普通条目,但不能上传多个图像,实际上甚至是单个图像。
我真的不知道如何调试 REST 部分,它什么也不返回。
我有这个数组来自视图:
Array
(
[1] => Array
(
[name] => sideshows.jpg
[type] => image/jpeg
[tmp_name] => /Applications/MAMP/tmp/php/phppYycdA
[error] => 0
[size] => 967656
)
[2] => Array
(
[name] => the-beer-scale.jpg
[type] => image/jpeg
[tmp_name] => /Applications/MAMP/tmp/php/phpCsiunQ
[error] => 0
[size] => 742219
)
[3] => Array
(
[name] => the-little-lace.jpg
[type] => image/jpeg
[tmp_name] => /Applications/MAMP/tmp/php/phpXjT7WL
[error] => 0
[size] => 939963
)
[4] => Array
(
[name] => varrstoen-australia.jpg
[type] => image/jpeg
[tmp_name] => /Applications/MAMP/tmp/php/phpcHrJXe
[error] => 0
[size] => 2204400
)
)
我正在使用我发现的这个帮助方法来整理多个文件上传:
function multifile_array()
{
if(count($_FILES) == 0)
return;
$files = array();
$all_files = $_FILES['files']['name'];
$i = 0;
foreach ($all_files as $filename) {
$files[++$i]['name'] = $filename;
$files[$i]['type'] = current($_FILES['files']['type']);
next($_FILES['files']['type']);
$files[$i]['tmp_name'] = current($_FILES['files']['tmp_name']);
next($_FILES['files']['tmp_name']);
$files[$i]['error'] = current($_FILES['files']['error']);
next($_FILES['files']['error']);
$files[$i]['size'] = current($_FILES['files']['size']);
next($_FILES['files']['size']);
}
$_FILES = $files;
}
这个函数在 API 控制器中被调用:
public function my_file_upload_post() {
if( ! $this->post('submit')) {
$this->response(NULL, 400);
}
$data = $this->post('data');
multifile_array();
$foldername = './uploads/' . $this->post('property_id');
if(!file_exists($foldername) && !is_dir($foldername)) {
mkdir($foldername, 0750, true);
}
$config['upload_path'] = $foldername;
$config['allowed_types'] = 'gif|jpg|png|doc|docx|pdf|xlsx|xls|txt';
$config['max_size'] = '10240';
$this->load->library('upload', $config);
foreach ($data as $file => $file_data) {
$this->upload->do_upload($file);
//echo '<pre>';
//print_r($this->upload->data());
//echo '</pre>';
}
if ( ! $this->upload->do_upload() ) {
return $this->response(array('error' => strip_tags($this->upload->display_errors())), 404);
} else {
$upload = $this->upload->data();
return $this->response($upload, 200);
}
}
我很高兴分享我的代码,我确实设法让多个文件上传,不用担心,但只是尝试使用 API 进行设置,以便我可以从外部网站、内部或 iPhone 调用它。帮助将不胜感激。
干杯
更新:
这是来自 API 控制器的代码:
function upload_post() {
$foldername = './uploads/' . $this->post('property_id');
if(!file_exists($foldername) && !is_dir($foldername)) {
mkdir($foldername, 0750, true);
}
$config['upload_path'] = $foldername;
$config['allowed_types'] = 'gif|jpg|png|doc|docx|pdf|xlsx|xls|txt';
$config['max_size'] = '10240';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
//return $this->response(array('error' => strip_tags($this->upload->display_errors())), 404);
return $this->response(array('error' => var_export($this->post('file'), true)), 404);
}
else
{
$data = array('upload_data' => $this->upload->data());
return $this->response(array('error' => $data['upload_data']), 404);
}
return $this->response(array('success' => 'successfully uploaded' ), 200);
}
如果您从表单直接访问 API,则此方法有效,但您需要在浏览器中输入用户名和密码。如果我通过控制器运行它,那么它找不到图像。