动态Codeigniter图像上传重命名

时间:2015-07-10 09:53:57

标签: php codeigniter file-upload

我使用codeigniter使用以下代码上传多个动态图像。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.availableIdentifier = [[NSMutableArray alloc] initWithObjects:@"Seg1",@"Seg2",@"Seg3", nil];


    [self switchView:self.navButtons[0]];
}

- (IBAction)switchView:(UIButton *)sender
{

    [self setSelectedIndexs:(int)sender.tag];

}

- (void)setSelectedIndexs:(int)index
{

    [self performSegueWithIdentifier:self.availableIdentifier[index] sender:self.navButtons[index]];

}

//Code of Custom Segue:
-(void)perform
{
    ViewController *controller = (ViewController *)self.sourceViewController;
    UIViewController *destController = (UIViewController *)self.destinationViewController;
    for (UIView *view in controller.containerView.subviews)
    {
        [view removeFromSuperview];
    }

    controller.currentController = destController;
    [controller.containerView addSubview:destController.view];

    [controller.containerView setTranslatesAutoresizingMaskIntoConstraints:NO];


    [destController didMoveToParentViewController:controller];
}

当我将图像上传到特定文件夹时,这可以正常工作;问题是,如果用户上传了具有相同名称的图像,那么它会自动重命名该图像并上传到该文件夹​​,但我想要的是通过添加foreach ($_FILES as $key => $value) { $imagePrefix = time(); if (!$this->upload->do_upload($key)) { echo $errors = $this->upload->display_errors(); return ''; } else { $imagename = $imagePrefix.$value['name']; $insertImage = $this->db->query("Insert into `tbl_event_imges` (`iEventID`,`vImage`) values ('".$insertId."','".$imagename."')"); } } 来重新命名,我在{{1}中添加了$imagePrefix部分代码,然后想要使用此名称上传图像。但这不适合我......

有什么建议吗?

3 个答案:

答案 0 :(得分:0)

您需要为上传功能提供配置首选项,如下所示:

$imagePrefix = time(); 
$imagename = $imagePrefix.$value['name'];

$this->load->library('upload');

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $imagename; // set the name here

$this->upload->initialize($config);

if (!$this->upload->do_upload($key)){
    ...
}else{
    ...
}

来源:http://www.codeigniter.com/user_guide/libraries/file_uploading.html?highlight=file%20upload#setting-preferences

答案 1 :(得分:0)

另外,您可以使用CI的重命名功能在文件上传后重命名。

$image = $this->upload->data();
$file_path = $image ['file_path'];
$file = $image ['full_path'];
$file_ext = $image['file_ext'];
$final_file_name = time() . $image['file_name'] . $file_ext;

答案 2 :(得分:0)

仅添加以下代码并成功运行,并将encrypt_name设置为false

$path = $_FILES['userfile']['name'];
$newName = "Add any name".".".pathinfo($path, PATHINFO_EXTENSION);
$config['file_name'] = $newName; 
$config['encrypt_name'] = false;