我想在每次上传多个文件时在计数索引之前插入下划线。
我的期望:
Schools.jpg
Schools_1.jpg
Schools_2.jpg
Schools_3.jpg
我的节目输出:
Schools.jpg
Schools1.jpg
Schools2.jpg
Schools3.jpg
这是我的代码:
控制器
public function do_upload() {
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++) {
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->db->insert('tbl_image', $file_array);
}
}
private function set_upload_options() {
$config = array(
'image_library' => 'gd2',
'file_name' => $this->input->post('file_name'),
'upload_path' => './public/img/uploads',
'upload_url' => base_url().'public/img/uploads',
'allowed_types' => 'jpg|jpeg|pdf',
'max_size' => '1024KB',
'max_width' => '1024',
'max_height' => '768',
'maintain_ratio'=> TRUE,
'overwrite' => false,
);
return $config;
}
HTML
<label for="file">Select File To Upload:</label>
<input type="file" name="userfile[]" multiple/>
<label for="file_name">Set image file name:</label>
<input type="text" size="20" name="file_name" required id="file_name"/>
<input type="submit" id="btnSubmit" value="Upload File"/>
答案 0 :(得分:1)
我看到了很多错误的建议答案,他们将下划线放在数组的索引中,就像这样
$_FILES['userfile']['name']= $files['userfile']['name'][$i]."_".[$i];
//you can't do this because $file['userfile']['name'][$i] is array so better do this code
$flag=0;
for($i=0; $i<$cpt; $i++) {
$flag++;
$_FILES['userfile']['name']=$files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options($i, $flag));
$this->db->insert('tbl_image', $file_array);
}
private function set_upload_options($i, $f) {
$charac= "";
if($f>1) {
$charac=$this->input->post('file_name')."_".$i;
//here is the code to add underscore for image 2 and above
}
else {
$charac=$this->input->post('file_name');
}
$config = array(
'image_library' => 'gd2',
'file_name' => $charac,
'upload_path' => './public/img/uploads',
'upload_url' => base_url().'public/img/uploads',
'allowed_types' => 'gif|jpg|jpeg',
'max_size' => '1024KB',
'max_width' => '1024',
'max_height' => '768',
'maintain_ratio'=> TRUE,
'overwrite' => false,
);
return $config;
}
答案 1 :(得分:1)
你可以通过修改上传库来做到这一点。如果文件存在,上传库会添加数字。你可以添加“_”来改变它。
打开codeigniter文件夹/system/Upload.php
在第403行用
$filename = str_replace($this->file_ext, '', $filename);
$filename = str_replace($this->file_ext, '_', $filename);
如果行号不相同,则搜索文本。 希望它会对你有所帮助。
答案 2 :(得分:0)
更改此行
for($i=0; $i<$cpt; $i++) {
$_FILES['userfile']['name']= $files['userfile']['name'][$i]."_".[$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->db->insert('tbl_image', $file_array);
}
答案 3 :(得分:0)
更改以下行:
$_FILES['userfile']['name']= $files['userfile']['name']."_".[$i];
到
$prefix = 'Schools'; // May also use $prefix = $_POST['prefix'];
$_FILES['userfile']['name']= str_replace($prefix, "Schools_" . $i, $files['userfile']['name'][$i])
答案 4 :(得分:0)
只需更改循环的第一行:
// find position of the last dot, so where the extension starts
$extension_pos = strrpos($files['userfile']['name'][$i], '.');
// insert underscore and numbering before filetype
$newname = substr($files['userfile']['name'][$i], 0, $extension_pos) . '_' . $i . substr($filename, $extension_pos);
// set new formated name to our file
$_FILES['userfile']['name'] = $newname;
或者您只能使用一行更改:
$_FILES['userfile']['name'] = preg_replace("/(\w+)\.(\w+)/","'\\1_".$i.".\\2'", $files['userfile']['name'][$i]);
它会在filetype之前添加下划线。