我正在做我的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');
}任何帮助都是非常感谢的。
发布于 2013-05-12 06:13:17
您可以使用以下命令检查$_FILES变量中的项数:
$total = count($_FILES['your_variable_array_in_html']['tmp_name']);您需要在执行以下操作之前执行此操作:
$this->upload->do_multi_upload("files");线路。
正如您已经注意到的,$_FILES只包含一个变量-一个数组-包含不同部分、tmp_name、name、error等的数组。有关更多详细信息,请查看manual。
发布于 2013-05-12 06:41:59
如果您想限制上传的文件。您可以将它们限制为两个步骤:
上传的文件数量
文件计数($_
‘’);
字符串'name‘可以替换为'tmp_name','error','size','type’
您使用的库在第一次执行时已将文件上传到您的上传路径中。所以你应该在上传之前手动检查它。
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
));
if (isset($_FILES['files']['name'])) {
$num = count($_FILES['files']['name']);
}
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');
}希望这能有所帮助
https://stackoverflow.com/questions/16501913
复制相似问题