在多个文件上传中,文件重命名的索引不会增加



我有用于多次上传的控制器代码,如下所示:

$numb_receipt = count($_FILES['receipt']['name']);
$dataArray = array();
for($i = 0; $i < $numb_receipt;$i++)
{
$today=date("dmY");
if(!empty($_FILES['receipt']['name'][$i])){

$_FILES['file']['name'] = $_FILES['receipt']['name'][$i];
$_FILES['file']['tmp_name'] = $_FILES['receipt']['tmp_name'][$i];
$_FILES['file']['type'] = $_FILES['receipt']['type'][$i];
$_FILES['file']['error'] = $_FILES['receipt']['error'][$i];
$_FILES['file']['size'] = $_FILES['receipt']['size'][$i];


$config['upload_path'] = './assets/cash/receipt/'; 
$config['allowed_types'] = 'jpg|jpeg|png|pdf';
$config['max_size'] = '5000';
$config['file_name'] = 'text_'.$i.'_'.$today;

$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$filename = 'assets/cash/receipt/'.$config['file_name'].'.'.$ext;
$filename = str_replace(" ", "_", $filename);
$this->load->library('upload',$config);


if (!file_exists($filename)){
echo "File not exist.";
if($this->upload->do_upload('file')){
$uploadData = $this->upload->data();
$receipt['receipt'] = $uploadData['file_name'];
}
}
else{
echo "File exist.";
}    
}           
}

但当我尝试打印$receipt['receipt'] = $uploadData['file_name']时输出总是显示:

text_0_15072020.png 
text_0_150720201.png
text_0_15072020.jpg

我需要更改什么才能使输出看起来像这样:

text_0_15072020.png
text_1_15072020.png
text_2_15072020.jpg

您必须重新初始化Codeigniter上传库

$config['upload_path'] = './assets/cash/receipt/'; 
$config['allowed_types'] = 'jpg|jpeg|png|pdf';
$config['max_size'] = '5000';
$this->load->library('upload',$config);
for($i = 0; $i < $numb_receipt;$i++)
{
$today=date("dmY");
if(!empty($_FILES['receipt']['name'][$i])){

$_FILES['file']['name'] = $_FILES['receipt']['name'][$i];
$_FILES['file']['tmp_name'] = $_FILES['receipt']['tmp_name'][$i];
$_FILES['file']['type'] = $_FILES['receipt']['type'][$i];
$_FILES['file']['error'] = $_FILES['receipt']['error'][$i];
$_FILES['file']['size'] = $_FILES['receipt']['size'][$i];

$config['file_name'] = 'text_'.$i.'_'.$today;
$this->upload->initialize($config); //enter new config here
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$filename = 'assets/cash/receipt/'.$config['file_name'].'.'.$ext;
$filename = str_replace(" ", "_", $filename);

if (!file_exists($filename)){
echo "File not exist.";
if($this->upload->do_upload('file')){
$uploadData = $this->upload->data();
$receipt['receipt'] = $uploadData['file_name'];
}
}
else{
echo "File exist.";
}    
}           
}

相关内容

  • 没有找到相关文章

最新更新