在codeigniter中使用修改后的名称上载多个文件类型

时间:2014-01-20 11:44:39

标签: codeigniter file-upload file-type file-rename

我正在尝试从同一表单向服务器上传mp3image类型的文件。我的控制器是:

$uniqueid = uniqid();
$rand = rand(99,199);
$site_string = 'melody';
$original_song_name =   strtolower($_FILES['file_song']['name']);
$original_song_name = str_replace(' ','_',$original_song_name);

$config['upload_path'] = './uploads/songs';
$config['allowed_types'] = 'mp3|gif|jpg|png';
$config['overwrite'] = false;
$config['file_name'] = $uniqueid.$rand.'_'.$original_song_name;

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

if ( ! $this->upload->do_upload('file_song'))
{
    $error = array('error' => $this->upload->display_errors());
    $this->load->view('admin/admin_add_song_view', $error);
}
else
{
    $data = array('song_upload_data' => $this->upload->data());
}
$poster = $_FILES['file_poster']['name'];


if($poster != ''){
    //$config['file_name'] = $uniqueid.$rand.'_'.$poster;
    if ( ! $this->upload->do_upload('file_poster'))
    {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('admin/admin_add_song_view', $error);
    }
    else
    {
        $pdata = array('poster_upload_data' => $this->upload->data());
    }
    $fileposter = $pdata['poster_upload_data']['file_name'];
}
var_dump($pdata);

我的var_dump($pdata)输出是:

array (size=1)
  'poster_upload_data' => 
    array (size=14)
      'file_name' => string '52dd0b2a4a20b103_sleep_away1.mp3' (length=32)
      'file_type' => string 'image/jpeg' (length=10)
      'file_path' => string 'C:/wamp/www/ciauth/uploads/songs/' (length=33)
      'full_path' => string 'C:/wamp/www/ciauth/uploads/songs/52dd0b2a4a20b103_sleep_away1.mp3' (length=65)
      'raw_name' => string '52dd0b2a4a20b103_sleep_away1' (length=28)
      'orig_name' => string '52dd0b2a4a20b103_sleep_away.mp3' (length=31)
      'client_name' => string 'Chrysanthemum.jpg' (length=17)
      'file_ext' => string '.mp3' (length=4)
      'file_size' => float 858.78
      'is_image' => boolean true
      'image_width' => int 1024
      'image_height' => int 768
      'image_type' => string 'jpeg' (length=4)
      'image_size_str' => string 'width="1024" height="768"' (length=25)

如您所见,$pdata['poster_upload_data']['file_name']是之前上传的mp3文件的文件名。如何将其更改为所需的image文件名?我试图在循环中添加$config['file_name'] = $uniqueid.$rand.'_'.$poster;,但它没有改变任何东西!

0 个答案:

没有答案