【发布时间】:2013-05-06 07:09:24
【问题描述】:
我正在开发我的 CodeIgniter 项目,到目前为止它运行良好。
但是,我需要一些方法来计算上传文件的数量,因为我想在某些情况下(但不是全部)限制它。
我该怎么做?我尝试了count($_FILES),但这没有给我任何可用的东西。
我还尝试了很多其他方法,但都没有给我所需的信息。
上传表单是一个多文件上传,我使用this库来处理多个上传。
没有计数的上传函数是这样的:
function do_upload()
{
$setid = $this->input->post('imageset');
$this->load->library('upload');
$this->load->library('image_lib');
$this->upload->initialize(array(
"upload_path" => "./photos/",
"allowed_types" => "jpg|jpeg|png|gif",
"encrypt_name" => TRUE
));
try {
$this->upload->do_multi_upload("files");
$images = $this->upload->get_multi_upload_data();
$config = array(
'image_library' => 'gd2',
'create_thumb' => TRUE,
'maintain_ratio' => TRUE,
'width' => '145',
'height' => '145'
);
foreach ($images as $image)
{
$config['source_image'] = $image['full_path'];
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->manage_model->insertimage($image['file_name'],$image['orig_name'],$image['file_size'],$image['file_type'],$setid);
}
$this->session->set_flashdata('success','Billederne er nu blevet uploadet.');
} catch (Exception $e) {
$this->session->set_flashdata('error', $e);
}
redirect('manage/images','refresh');
}
非常感谢任何帮助。
【问题讨论】:
-
count($images)设置该变量后。 -
@jeroen:问题是如果我通过
$images变量统计图片,然后报错,图片已经上传到服务器,我要统计在那之前。 -
如果你想避免上传,你需要在客户端使用javascript(也许结合php使用ajax)。
-
@jeroen:我不一定要避免上传部分 - 只要图像保留在 tmp 文件夹中,而不是在 upload_path 中。
-
@crypticツ 因为无论上传多少文件,这都会给我 1。可能与 PHP 用于此类事情的多维数组有关。
标签: php codeigniter file-upload